diff --git a/SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-1/01-TGT.png b/SDL2-Bits-And-Pieces/Experiment-01/01-TGT.png similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-1/01-TGT.png rename to SDL2-Bits-And-Pieces/Experiment-01/01-TGT.png diff --git a/SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-1/SDL2-Experiment-01.c b/SDL2-Bits-And-Pieces/Experiment-01/SDL2-Experiment-01.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-1/SDL2-Experiment-01.c rename to SDL2-Bits-And-Pieces/Experiment-01/SDL2-Experiment-01.c diff --git a/SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-2/02-TGT.png b/SDL2-Bits-And-Pieces/Experiment-02/02-TGT.png similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-2/02-TGT.png rename to SDL2-Bits-And-Pieces/Experiment-02/02-TGT.png diff --git a/SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-2/SDL2-Experiment-02.c b/SDL2-Bits-And-Pieces/Experiment-02/SDL2-Experiment-02.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-2/SDL2-Experiment-02.c rename to SDL2-Bits-And-Pieces/Experiment-02/SDL2-Experiment-02.c diff --git a/SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-3/SDL2-Experiment-03.c b/SDL2-Bits-And-Pieces/Experiment-03/SDL2-Experiment-03.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-01-03-Basics/Experiment-3/SDL2-Experiment-03.c rename to SDL2-Bits-And-Pieces/Experiment-03/SDL2-Experiment-03.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-4/SDL2-Experiment-04.c b/SDL2-Bits-And-Pieces/Experiment-04/SDL2-Experiment-04.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-4/SDL2-Experiment-04.c rename to SDL2-Bits-And-Pieces/Experiment-04/SDL2-Experiment-04.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-5/SDL2-Experiment-05.c b/SDL2-Bits-And-Pieces/Experiment-05/SDL2-Experiment-05.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-5/SDL2-Experiment-05.c rename to SDL2-Bits-And-Pieces/Experiment-05/SDL2-Experiment-05.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-6/SDL2-Experiment-06.c b/SDL2-Bits-And-Pieces/Experiment-06/SDL2-Experiment-06.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-6/SDL2-Experiment-06.c rename to SDL2-Bits-And-Pieces/Experiment-06/SDL2-Experiment-06.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-7/SDL2-Experiment-07.c b/SDL2-Bits-And-Pieces/Experiment-07/SDL2-Experiment-07.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-7/SDL2-Experiment-07.c rename to SDL2-Bits-And-Pieces/Experiment-07/SDL2-Experiment-07.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-8/SDL2-Experiment-08.c b/SDL2-Bits-And-Pieces/Experiment-08/SDL2-Experiment-08.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-8/SDL2-Experiment-08.c rename to SDL2-Bits-And-Pieces/Experiment-08/SDL2-Experiment-08.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-9/SDL2-Experiment-09.c b/SDL2-Bits-And-Pieces/Experiment-09/SDL2-Experiment-09.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/Experiment-9/SDL2-Experiment-09.c rename to SDL2-Bits-And-Pieces/Experiment-09/SDL2-Experiment-09.c diff --git a/SDL2-Bits-And-Pieces/SDL2-Notes.org b/SDL2-Bits-And-Pieces/SDL2-Notes.org index b2fc704..7a7e5a2 100644 --- a/SDL2-Bits-And-Pieces/SDL2-Notes.org +++ b/SDL2-Bits-And-Pieces/SDL2-Notes.org @@ -1,14 +1,9 @@ -* Experiments 1-3: Basics: -These experiments are mostly about learning to put things on the screen. - Experiment 01: A "Radar" which tracks the position of the mouse, drawn using SDL2 primitives and a loaded image. - Experiment 02: A stripped down version of the first experiment, using keyboard input to move the cursor. - Experiment 03: An improved version of 02, which uses movement acceleration to make it feel better to use. - -* Experiment 4-8: Gravity -These experiments are based around developing a gravity simulation. - Experiment 04: An example of simple gravity and bouncing simulation. - Experiment 05: A "Spacewar" style gravity simulation, with a ship orbiting a star. - Experiment 06: A modified version of 05, using delta-time scaling so it is @@ -19,3 +14,9 @@ These experiments are based around developing a gravity simulation. "star." Fun to play aronud with. - Experiment 09: A version of the simulation where the ship can be steered and accelerated. +- Experiment 10: A version of the simulation where the ship has been given a + sprite. +- Experiment 11: A version of the simulation where the sprite animates while + accelerating. +- Experiment 12: A version of the simulation where the screen scrolls around + with the movement of the ship. diff --git a/SDL2-Bits-And-Pieces/SDL2-UDP-Experiment-01-controller.c b/SDL2-Bits-And-Pieces/SDL2-UDP-Experiment-01-controller.c new file mode 100644 index 0000000..21b0c57 --- /dev/null +++ b/SDL2-Bits-And-Pieces/SDL2-UDP-Experiment-01-controller.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include +typedef struct position +{ + int x; + int y; +} position; + +int main() +{ + // Create the socket: + int socketFileDesc = socket(AF_INET, SOCK_DGRAM, 0); + if (socketFileDesc < 0) + { + fprintf(stderr, "\tSocket Creation is:\t\033[33;40mRED.\033[0m Aborting launch.\n"); + exit(0); + } + printf("\tSocket Creation is:\t\033[32;40mGREEN.\033[0m\n"); + + // Create and fill the information needed to bind to the socket: + struct sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; // IPv4 + serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); + serverAddress.sin_port = htons(12000); + + position posToSend = {0, 0}; + + // Send data at the server: + while (1) + { + scanf("%d %d", &posToSend.x, &posToSend.y); + sendto(socketFileDesc, &posToSend, sizeof(posToSend), 0, (const struct sockaddr *)&serverAddress, sizeof(serverAddress)); + } +} diff --git a/SDL2-Bits-And-Pieces/SDL2-UDP-Experiment-01.c b/SDL2-Bits-And-Pieces/SDL2-UDP-Experiment-01.c new file mode 100644 index 0000000..0bcf93e --- /dev/null +++ b/SDL2-Bits-And-Pieces/SDL2-UDP-Experiment-01.c @@ -0,0 +1,147 @@ +// SDL/UDP Experiment 01, Barra Ó Catháin. +// ======================================= +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius) +{ + const int32_t diameter = (radius * 2); + + int32_t x = (radius - 1); + int32_t y = 0; + int32_t tx = 1; + int32_t ty = 1; + int32_t error = (tx - diameter); + + while (x >= y) + { + // Each of the following renders an octant of the circle + SDL_RenderDrawPoint(renderer, centreX + x, centreY - y); + SDL_RenderDrawPoint(renderer, centreX + x, centreY + y); + SDL_RenderDrawPoint(renderer, centreX - x, centreY - y); + SDL_RenderDrawPoint(renderer, centreX - x, centreY + y); + SDL_RenderDrawPoint(renderer, centreX + y, centreY - x); + SDL_RenderDrawPoint(renderer, centreX + y, centreY + x); + SDL_RenderDrawPoint(renderer, centreX - y, centreY - x); + SDL_RenderDrawPoint(renderer, centreX - y, centreY + x); + + if (error <= 0) + { + ++y; + error += ty; + ty += 2; + } + + if (error > 0) + { + --x; + tx += 2; + error += (tx - diameter); + } + } +} + +// Get the largest radius for a circle that can fit in the width and height of a rectangle: +static inline int getRadius(int width, int height) +{ + return (width/2 < height/2) ? width/2 : height/2; +} + + +int main(int argc, char ** argv) +{ + SDL_Event event; + int positionX = 0, positionY = 0; + uint32_t rendererFlags = SDL_RENDERER_ACCELERATED; + + // Initialize the SDL library, video, sound, and input: + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) + { + printf("SDL Initialization Error: %s\n", SDL_GetError()); + } + + // Create an SDL window and rendering context in that window: + SDL_Window * window = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 640, 0); + SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags); + + // Enable resizing the window: + SDL_SetWindowResizable(window, SDL_TRUE); + + // Create the socket: + int socketFileDesc = socket(AF_INET, SOCK_DGRAM, 0); + if (socketFileDesc < 0) + { + fprintf(stderr, "\tSocket Creation is:\t\033[33;40mRED.\033[0m Aborting launch.\n"); + exit(0); + } + printf("\tSocket Creation is:\t\033[32;40mGREEN.\033[0m\n"); + + // Make the socket timeout: + struct timeval read_timeout; + read_timeout.tv_sec = 0; + read_timeout.tv_usec = 10; + setsockopt(socketFileDesc, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof read_timeout); + + // Create and fill the information needed to bind to the socket: + struct sockaddr_in serverAddress; + memset(&serverAddress, 0, sizeof(serverAddress)); + serverAddress.sin_family = AF_INET; // IPv4 + serverAddress.sin_addr.s_addr = INADDR_ANY; + serverAddress.sin_port = htons(12000); + + // Bind to the socket: + if (bind(socketFileDesc, (const struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) + { + perror("bind failed"); + exit(EXIT_FAILURE); + } + + // A struct to transfer the position data: + typedef struct position + { + int x; + int y; + } position; + + position currentPosition = {0, 0}; + + while (true) + { + // Receive data from the socket: + recvfrom(socketFileDesc, ¤tPosition, sizeof(position), 0, NULL, NULL); + + // Set the position to the received one: + positionX = currentPosition.x; + positionY = currentPosition.y; + + // Set the colour to black: + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + + // Clear the screen, filling it with black: + SDL_RenderClear(renderer); + + // Set the colour to yellow: + SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); + + // Draw a circle around the position pointer: + DrawCircle(renderer, positionX, positionY, 15); + + // Present the rendered graphics: + SDL_RenderPresent(renderer); + + // Delay enough so that we run at 144 frames: + SDL_Delay(1000 / 144); + } + return 0; +} +// ============================================================================= +// Local Variables: +// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-UDP-Experiment-01.c" +// End: