From 53a88025cd5bb2aaeb300498d1ed805f6159103b Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Wed, 15 Mar 2023 23:22:16 +0000 Subject: [PATCH] Added acceleration to make it feel better to use. --- 02-TGT.png | Bin 613 -> 0 bytes SDL2-Experiment-02.c => SDL2-Experiment-03.c | 146 +++++++++++++++---- 2 files changed, 118 insertions(+), 28 deletions(-) delete mode 100644 02-TGT.png rename SDL2-Experiment-02.c => SDL2-Experiment-03.c (65%) diff --git a/02-TGT.png b/02-TGT.png deleted file mode 100644 index a4804e1843a6bc06c450d8090f4ffef9e1da4dd3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 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 @@ -55,26 +54,20 @@ 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; + int width = 0, height = 0; uint32_t rendererFlags = SDL_RENDERER_ACCELERATED; - + long positionX = 320, positionY = 320, velocityX = 0, velocityY = 0; + bool accelLeft = false, accelRight = false, accelUp = false, accelDown = false; + // Initialize the SDL library, video, sound, and input: 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); @@ -91,28 +84,61 @@ int main(int argc, char ** argv) quit = true; break; } + // Begin accelerating in the direction that is pressed: case SDL_KEYDOWN: { switch (event.key.keysym.sym) { case SDLK_LEFT: { - velocityX += -1; + accelLeft = true; break; } case SDLK_RIGHT: { - velocityX += 1; + accelRight = true; break; } case SDLK_UP: { - velocityY += -1; + accelUp = true; break; } case SDLK_DOWN: { - velocityY += 1; + accelDown = true; + break; + } + default: + { + break; + } + } + break; + } + // Stop accelerating in the direction that is released: + case SDL_KEYUP: + { + switch (event.key.keysym.sym) + { + case SDLK_LEFT: + { + accelLeft = false; + break; + } + case SDLK_RIGHT: + { + accelRight = false; + break; + } + case SDLK_UP: + { + accelUp = false; + break; + } + case SDLK_DOWN: + { + accelDown = false; break; } default: @@ -129,9 +155,81 @@ int main(int argc, char ** argv) } } + // Accelerate: + if(accelLeft) + { + velocityX -= 2; + } + + if(accelRight) + { + velocityX += 2; + } + + if(accelUp) + { + velocityY -= 2; + } + + if(accelDown) + { + velocityY += 2; + } + + // Limit velocity: + if(velocityX > 10) + { + velocityX = 10; + } + if(velocityY > 10) + { + velocityY = 10; + } + if(velocityX < -10) + { + velocityX = -10; + } + if(velocityY < -10) + { + velocityY = -10; + } + + // Deccelerate: + if(velocityX != 0) + { + velocityX = (velocityX < 0) ? velocityX + 1 : velocityX - 1; + } + + if(velocityY != 0) + { + velocityY = (velocityY < 0) ? velocityY + 1 : velocityY - 1; + } + // Move the position: - positionX += velocityX; - positionY += velocityY; + if(velocityX != 0) + { + positionX += velocityX; + if(positionX < 0) + { + positionX = 0; + } + if(positionX > 640) + { + positionX = 640; + } + } + if(velocityY != 0) + { + positionY += velocityY; + if(positionY < 0) + { + positionY = 0; + } + if(positionY > 640) + { + positionY = 640; + } + } // Store the window's current width and height: SDL_GetWindowSize(window, &width, &height); @@ -151,23 +249,15 @@ int main(int argc, char ** argv) // 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); + SDL_Delay(1000 / 60); } return 0; } // =========================================================================================== // Local Variables: -// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-02.c -lSDL2_image -lm" +// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-03.c -lm" // End: