From a0fbdce3712ea86ae829fac2ba57bdb888d5a259 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Tue, 7 Feb 2023 16:30:23 +0000 Subject: [PATCH] Added SDL2 Experiment 11 A test of simple sprite animation. This will allow my Spacewar clone to have actual animated ships. --- .../11-Ship-Accelerating-Frame-2.png | Bin 0 -> 6696 bytes SDL2-Bits-And-Pieces/11-Ship-Accelerating.png | Bin 0 -> 1023 bytes .../11-Ship-Anticlockwise.png | Bin 0 -> 992 bytes SDL2-Bits-And-Pieces/11-Ship-Clockwise.png | Bin 0 -> 1004 bytes SDL2-Bits-And-Pieces/11-Ship-Idle.png | Bin 0 -> 981 bytes SDL2-Bits-And-Pieces/SDL2-Experiment-11.c | 330 ++++++++++++++++++ 6 files changed, 330 insertions(+) create mode 100644 SDL2-Bits-And-Pieces/11-Ship-Accelerating-Frame-2.png create mode 100644 SDL2-Bits-And-Pieces/11-Ship-Accelerating.png create mode 100644 SDL2-Bits-And-Pieces/11-Ship-Anticlockwise.png create mode 100644 SDL2-Bits-And-Pieces/11-Ship-Clockwise.png create mode 100644 SDL2-Bits-And-Pieces/11-Ship-Idle.png create mode 100644 SDL2-Bits-And-Pieces/SDL2-Experiment-11.c diff --git a/SDL2-Bits-And-Pieces/11-Ship-Accelerating-Frame-2.png b/SDL2-Bits-And-Pieces/11-Ship-Accelerating-Frame-2.png new file mode 100644 index 0000000000000000000000000000000000000000..ec49319d406a615eed192c809b509ce90c67c26f GIT binary patch literal 6696 zcmeHKcTiK?w+={G5a}Xd42l%f69|zay>}2n5mQb=1QJYw2}PucA|itA3aFqIK?tCL z6bm2-7C@0plVS(4P(($pBJTv$>z((Td2?sp??02ABxmh!e`~F8uf1oTgYK?&vNGy2 z5C}xp!QR>v{DlfHDGBhK6B+pd0+9-g^xnetB=MkOY!;0X0zkPDVE`21GiVS9|B+zJ z){`&v<-XFDE5tTQHM03XNoTrG?UcdZqG!eUEmg3RZ`)-R8=csCL}#^3CRf#oS@(ls!?D9ldOv$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ode 100644 index 0000000000000000000000000000000000000000..f9b25952c54810cff1469d75726101c2a1c2aa88 GIT binary patch literal 1023 zcmVEX>4Tx04R}tkv&MmKpe$iQ?()$2djv5$WWc^q9Ts93Pq?8YK2xEOfLO`CJjl8 zi=*ILaPVWX>fqw6tAnc`2!4RLx;QDiNQwVT3N2zhIPS;0dyl(!fY2y2&FYE)nqD! zgY!Odh!tg(_?&pcqze*1a$RxxjdQ_afoF!ybZVYBL@X9MSm|I^G&SOB;)ts0lrLml zRyl8R*2*>3x+i~OFsH99bDd@w2`pj>5=1Ddp@cH3h|;Q)Vj)HQaS#8f(i|qi@OrgSSB6n%i4zAEysMnz~Bf00)P_ zc!9FlJ>K2h+1tNoTK)Y1U~6)pRr#=z00006VoOIv0AK*_0QhigOSS+2010qNS#tmY zE+YT{E+YYWr9XB6000McNliru=K=;57#D*Z@go2L02y>eSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{00GxYL_t(o!_`+yZi6roow10TN-a{eVaKW~luP&~?j>>v zE`e|217w?JF5nH7Xcx3;m~Jpihz&NRjq+9&9%kN*=VJrR0JWH-BbsCUb+zf%h=^14 z;9TPd`!a_qBpBQn_e)@3=GOJqfQS(yGFJ{1MPZ7dTVJ&bi0H>bY+=??$`FUpMv?_a zWl2Pbsg)Cy1GXkZMDo_Bem#k@;qY3Y6lQ9Ntp_fvE5aL4K$Tm%LwDk#Q zVcfX9@^aE@!9Z$YBt(5A%rD+Or979!A5XUj> zcDruh9EI|U01<1#EC~K6fC0(Tv^~t!50|1D!egpt; t8K3+%6lHByC|67T*ZdJ>O)fIN_yxfglYdt&D2)IB002ovPDHLkV1jZO!7l&+ literal 0 HcmV?d00001 diff --git a/SDL2-Bits-And-Pieces/11-Ship-Anticlockwise.png b/SDL2-Bits-And-Pieces/11-Ship-Anticlockwise.png new file mode 100644 index 0000000000000000000000000000000000000000..22268ea46797a47a590bcbafcb666ec29d12cf15 GIT binary patch literal 992 zcmV<610Vc}P)EX>4Tx04R}tkv&MmKpe$iQ?()$2djv5$WWc^q9Ts93Pq?8YK2xEOfLO`CJjl8 zi=*ILaPVWX>fqw6tAnc`2!4RLx;QDiNQwVT3N2zhIPS;0dyl(!fY2y2&FYE)nqD! zgY!Odh!tg(_?&pcqze*1a$RxxjdQ_afoF!ybZVYBL@X9MSm|I^G&SOB;)ts0lrLml zRyl8R*2*>3x+i~OFsH99bDd@w2`pj>5=1Ddp@cH3h|;Q)Vj)HQaS#8f(i|qi@OrgSSB6n%i4zAEysMnz~Bf00)P_ zc!9FlJ>K2h+1tNoTK)Y1U~6)pRr#=z00006VoOIv0AK*_0QhigOSS+2010qNS#tmY zE+YT{E+YYWr9XB6000McNliru=K=;57dWp{(6s;n02y>eSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{00Fp3L_t(o!_`(hZo@DPB~=H=PK=IK>Xm zz1-taE12ShfIH)H9XQk~c)l4BF+)TVtAXuy>zklE-?R&u`8(p+!k@L)V*+Bu_YAT(r{x#^=iB=dG+>C6IA3g_I((|bN-`~@NEwbme|ywdl&&bd=)PKbcV zE*2dT4N^@+Fs2|yDvc?Z4>>Wp2#E-kwP1|tt$eLD7-K+L3#kr^lt`7euqldU1~x?j z%GxAmM5@8DNd4!rb^#HAlrk!pQo@*Gy>Nh;c~@0`9`UZKF6!X%)^RKiQ|RwT@%e`9 zL#VYv@-bbLwR8HDh(IM8Hf_YQ)*Euv&jV~99nYyfZH(!@m1PNep2P8Yggnn-zu$Lq zp9+)005iW2DS5-cw_@ng(I*cUValSx7T)-Jy}r^Z%d)%pTz@DHcW*;L#0B?2){O|g zaALS<&ALg}odsDpPm%9F2)@}Ml67;HTfJJIV*0OdANNghU-bWC7yJMa+=#Dfm9;_u O0000V literal 0 HcmV?d00001 diff --git a/SDL2-Bits-And-Pieces/11-Ship-Clockwise.png b/SDL2-Bits-And-Pieces/11-Ship-Clockwise.png new file mode 100644 index 0000000000000000000000000000000000000000..aa9989a921ffe1c65205a28da12626185ea3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npw> zWFU8GbZ8()Nlj2!fese{00FH@L_t(o!|hhPZNo4Sy<=n`MWP~&I#sCzFL9f23J<|0 z%uP6eTbEvdjS3@$ki@(+qTmlHk&=VJKwd@YPWO4d4*?c{dn~b0EOGw+wi(Wdh;!`V zQs*9rR>7Pg1pF}`H-JN{g6F#d5eq~Vu^QNJx4sF6^If}unZF}}t^8SQos$rTf)tso zJTvpx+|J3WfzXg)=5CDYkj&eOU1t%rY+7pSK|(;)+Kg+wLj! z!inL^_G_x{ZC|%tk;>3@kavM>I}!Pg88%k%j|uz$F2a3?5KH$N00000NkvXXu0mjf Dp;@&J literal 0 HcmV?d00001 diff --git a/SDL2-Bits-And-Pieces/SDL2-Experiment-11.c b/SDL2-Bits-And-Pieces/SDL2-Experiment-11.c new file mode 100644 index 0000000..065ee93 --- /dev/null +++ b/SDL2-Bits-And-Pieces/SDL2-Experiment-11.c @@ -0,0 +1,330 @@ +// SDL Experiment 11, Barra Ó Catháin. +// =================================== +#include +#include +#include +#include +#include +#include + +typedef struct xyVector +{ + double xComponent; + double yComponent; +} xyVector; + +// Calculate the vector from point A to point B: +static inline void xyVectorBetweenPoints(long ax, long ay, long bx, long by, xyVector * vector) +{ + vector->xComponent = bx - ax; + vector->yComponent = by - ay; +} + +// Normalize a vector, returning the magnitude: +static inline double normalizeXYVector(xyVector * vector) +{ + double magnitude = sqrt(pow(vector->xComponent, 2) + pow(vector->yComponent, 2)); + vector->xComponent /= magnitude; + vector->yComponent /= magnitude; + return magnitude; +} + +// Get the angle between vectors: +static inline double angleBetweenVectors(xyVector * vectorA, xyVector * vectorB) +{ + double dotProduct = (vectorA->xComponent * vectorB->xComponent) + (vectorA->yComponent * vectorB->yComponent); + double determinant = (vectorA->xComponent * vectorB->yComponent) - (vectorA->yComponent * vectorB->xComponent); + + return atan2(dotProduct, determinant) / 0.01745329; +} + +static inline void rotateXYVector(xyVector * vector, double degrees) +{ + double xComponent = vector->xComponent, yComponent = vector->yComponent; + vector->xComponent = (cos(degrees * 0.01745329) * xComponent) - (sin(degrees * 0.01745329) * yComponent); + vector->yComponent = (sin(degrees * 0.01745329) * xComponent) + (cos(degrees * 0.01745329) * yComponent); +} + +// Add vector B to vector A: +static inline void addXYVector(xyVector * vectorA, xyVector * vectorB) +{ + vectorA->xComponent += vectorB->xComponent; + vectorA->yComponent += vectorB->yComponent; +} + +// Add vector B to vector A, scaled for units per frame: +static inline void addXYVectorDeltaScaled(xyVector * vectorA, xyVector * vectorB, double deltaTime) +{ + vectorA->xComponent += vectorB->xComponent * (0.001 * deltaTime) * 60; + vectorA->yComponent += vectorB->yComponent * (0.001 * deltaTime) * 60; +} + +// Multiply a vector by a scalar constant: +static inline void multiplyXYVector(xyVector * vector, double scalar) +{ + vector->xComponent *= scalar; + vector->yComponent *= scalar; +} +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); + } + } +} + +int main(int argc, char ** argv) +{ + SDL_Event event; + int width = 0, height = 0; + long positionX = 0, positionY = 0; + uint32_t rendererFlags = SDL_RENDERER_ACCELERATED; + uint64_t thisFrameTime = SDL_GetPerformanceCounter(), lastFrameTime = 0; + double deltaTime = 0, gravityMagnitude = 0, gravityAcceleration = 0, frameAccumulator = 0; + bool quit = false, rotatingClockwise = false, rotatingAnticlockwise = false, accelerating = false; + xyVector positionVector = {100, 100}, velocityVector = {0, 0}, gravityVector = {0, 0}, engineVector = {0.08, 0}, upVector = {0, 0.1}; + + // Initialize the SDL library, video, sound, and input: + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) + { + printf("SDL Initialization Error: %s\n", SDL_GetError()); + } + + // Initialize image loading: + IMG_Init(IMG_INIT_PNG); + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); + + // Create a rectangle to put the ship in: + SDL_Rect shipRect; + shipRect.w = 32; + shipRect.h = 32; + + // 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); + + // Load in all of our textures: + SDL_Texture * idleTexture, * acceleratingTexture, * clockwiseTexture, * anticlockwiseTexture, * currentTexture, + * acceleratingTexture2; + + idleTexture = IMG_LoadTexture(renderer, "./11-Ship-Idle.png"); + clockwiseTexture = IMG_LoadTexture(renderer, "./11-Ship-Clockwise.png"); + acceleratingTexture = IMG_LoadTexture(renderer, "./11-Ship-Accelerating.png"); + anticlockwiseTexture = IMG_LoadTexture(renderer, "./11-Ship-Anticlockwise.png"); + acceleratingTexture2 = IMG_LoadTexture(renderer, "./11-Ship-Accelerating-Frame-2.png"); + + // Enable resizing the window: + SDL_SetWindowResizable(window, SDL_TRUE); + + while (!quit) + { + lastFrameTime = thisFrameTime; + thisFrameTime = SDL_GetPerformanceCounter(); + deltaTime = (double)(((thisFrameTime - lastFrameTime) * 1000) / (double)SDL_GetPerformanceFrequency()); + + // 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: + { + rotatingAnticlockwise = true; + break; + } + case SDLK_RIGHT: + { + rotatingClockwise = true; + break; + } + case SDLK_UP: + { + accelerating = true; + break; + } + default: + { + break; + } + } + break; + } + case SDL_KEYUP: + { + switch (event.key.keysym.sym) + { + case SDLK_LEFT: + { + rotatingAnticlockwise = false; + break; + } + case SDLK_RIGHT: + { + rotatingClockwise = false; + break; + } + case SDLK_UP: + { + accelerating = false; + frameAccumulator = 0; + break; + } + default: + { + break; + } + } + break; + } + default: + { + break; + } + } + } + + // Store the window's current width and height: + SDL_GetWindowSize(window, &width, &height); + + // Calculate the vector between the star and ship: + xyVectorBetweenPoints(positionVector.xComponent, positionVector.yComponent, width/2, height/2, &gravityVector); + + // Make it into a unit vector: + gravityMagnitude = normalizeXYVector(&gravityVector); + + // Calculate the gravity between the star and ship: + gravityAcceleration = (gravityMagnitude >= 45) ? + (2500 / pow(gravityMagnitude, 2)): + 0; + + // Scale the vector: + multiplyXYVector(&gravityVector, gravityAcceleration); + + // Wrap the position if the ship goes off-screen: + if(positionVector.xComponent > width + 15) + { + positionVector.xComponent = 0; + velocityVector.xComponent *= 0.6; + } + if(positionVector.yComponent > height + 15) + { + positionVector.yComponent = 0; + velocityVector.yComponent *= 0.6; + } + if(positionVector.xComponent < -15) + { + positionVector.xComponent = width; + velocityVector.xComponent *= 0.6; + } + if(positionVector.yComponent < -15) + { + positionVector.yComponent = height; + velocityVector.yComponent *= 0.6; + } + + // Set the texture to idle: + currentTexture = idleTexture; + + // Rotate the engine vector if needed: + if(rotatingClockwise) + { + rotateXYVector(&engineVector, 0.1 * deltaTime); + currentTexture = clockwiseTexture; + } + if(rotatingAnticlockwise) + { + rotateXYVector(&engineVector, -0.1 * deltaTime); + currentTexture = anticlockwiseTexture; + } + + // Calculate the new current velocity: + addXYVectorDeltaScaled(&velocityVector, &gravityVector, deltaTime); + if(accelerating) + { + addXYVectorDeltaScaled(&velocityVector, &engineVector, deltaTime); + frameAccumulator += deltaTime; + currentTexture = acceleratingTexture; + if((long)frameAccumulator % 4) + { + currentTexture = acceleratingTexture2; + } + } + + // Calculate the new position: + addXYVectorDeltaScaled(&positionVector, &velocityVector, deltaTime); + + positionX = (long)positionVector.xComponent; + positionY = (long)positionVector.yComponent; + + // Calculate the position of the sprite: + shipRect.x = positionX - 15; + shipRect.y = positionY - 15; + + // Set the colour to black: + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + + // Clear the screen, filling it with black: + SDL_RenderClear(renderer); + + // Draw the ship: + SDL_RenderCopyEx(renderer, currentTexture, NULL, &shipRect, angleBetweenVectors(&engineVector, &upVector) + 90, NULL, 0); + + // Set the colour to yellow: + SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); + + // Draw a circle in the center as the star: + DrawCircle(renderer, width/2, height/2, 30); + + // Draw a line representing the velocity: + SDL_RenderDrawLine(renderer, positionX, positionY, + (long)(positionVector.xComponent + velocityVector.xComponent * 15), + (long)(positionVector.yComponent + velocityVector.yComponent * 15)); + + // Present the rendered graphics: + SDL_RenderPresent(renderer); + } + return 0; +} +// =========================================================================================== +// Local Variables: +// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-11.c -lSDL2_image -lm" +// End: