From c46e98009268946cf5f901f0bc3bcb89bc607d67 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Sun, 29 Jan 2023 22:05:45 +0000 Subject: [PATCH] Added SDL2 Experiment 2 Learning how to take input from the keyboard. --- SDL2-Bits-And-Pieces/02-TGT.png | Bin 0 -> 613 bytes SDL2-Bits-And-Pieces/SDL2-Experiment-02.c | 178 ++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 SDL2-Bits-And-Pieces/02-TGT.png create mode 100644 SDL2-Bits-And-Pieces/SDL2-Experiment-02.c diff --git a/SDL2-Bits-And-Pieces/02-TGT.png b/SDL2-Bits-And-Pieces/02-TGT.png new file mode 100644 index 0000000000000000000000000000000000000000..a4804e1843a6bc06c450d8090f4ffef9e1da4dd3 GIT binary patch literal 613 zcmV-r0-F7aP)EX>4Tx04R}tkv&MmP!xqv(@I4u4i*t{$WWc^q9Ts93Pq?8YK2xEOkVm2O&XFE z7e~Rh;NZ_<)xpJCR|i)?5c~mgb#YR3krMAq3N2#1@OU5R-E(;FK0s(znP!C&fTr7K zCY2O(`BgFeiU1-QLI`o0S;m|srQus&_tZ^w7w1|2eScQJTCf-p5Q!7aFm2)u;+aj` z;Ji;9Wo204LswCDE&Y}j(70+9Fj00006VoOIv0RI600RN!9r;`8x010qNS#tmY zE+YT{E+YYWr9XB6000McNliru=K%~B1`-Wx9-aUI02y>eSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{002EnL_t(2&tv@0!0?{|4KOk=FrtfN6GH*$lK2!O%P}!f z2xxD>e+GvC_`E}m3pj{!3cf(ZC&xmHq1XTbTNfGt +#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; + bool quit = false; + int width = 0, height = 0, positionX = 0, positionY = 0, velocityX = 0, velocityY = 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()); + } + + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) + { + printf("SDL Initialization Error: %s\n", SDL_GetError()); + } + + IMG_Init(IMG_INIT_PNG); + + // 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); + + SDL_Texture * targetTexture; + targetTexture = IMG_LoadTexture(renderer, "./02-TGT.png"); + SDL_Rect targetText; + targetText.w = 16; + targetText.h = 8; + + // Enable resizing the window: + SDL_SetWindowResizable(window, SDL_TRUE); + + while (!quit) + { + // Check if the user wants to quit: + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + { + quit = true; + break; + } + case SDL_KEYDOWN: + { + switch (event.key.keysym.sym) + { + case SDLK_LEFT: + { + velocityX += -1; + break; + } + case SDLK_RIGHT: + { + velocityX += 1; + break; + } + case SDLK_UP: + { + velocityY += -1; + break; + } + case SDLK_DOWN: + { + velocityY += 1; + break; + } + default: + { + break; + } + } + break; + } + default: + { + break; + } + } + } + + // Move the position: + positionX += velocityX; + positionY += velocityY; + + // Store the window's current width and height: + SDL_GetWindowSize(window, &width, &height); + + // 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 line from the center of the window to the position pointer: + SDL_RenderDrawLine(renderer, width/2, height/2, positionX, positionY); + + // Draw a circle around the position pointer: + DrawCircle(renderer, positionX, positionY, 15); + + // Set the rect at the correct position to put the TGT down: + targetText.x = positionX + 20; + targetText.y = positionY - 4; + if(positionX < width/2) + { + targetText.x = positionX - 36; + } + SDL_RenderCopy(renderer, targetTexture, NULL, &targetText); + // 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-Experiment-02.c -lSDL2_image -lm" +// End: