From 0b1ea56db7f0315a6115fe859853613cac12842d Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Thu, 16 Mar 2023 00:12:22 +0000 Subject: [PATCH] Added a texture for the ship. --- 10-Ship-Accelerating.png | Bin 0 -> 1023 bytes 10-Ship-Anticlockwise.png | Bin 0 -> 992 bytes 10-Ship-Clockwise.png | Bin 0 -> 1004 bytes 10-Ship-Idle.png | Bin 0 -> 981 bytes Spacewar.c | 66 +++++++++++++++++++++++++++----------- 5 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 10-Ship-Accelerating.png create mode 100644 10-Ship-Anticlockwise.png create mode 100644 10-Ship-Clockwise.png create mode 100644 10-Ship-Idle.png diff --git a/10-Ship-Accelerating.png b/10-Ship-Accelerating.png new file mode 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/10-Ship-Anticlockwise.png b/10-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/10-Ship-Clockwise.png b/10-Ship-Clockwise.png new file mode 100644 index 0000000000000000000000000000000000000000..aa9989a921ffe1c65205a28da12626185ea3256e GIT binary patch literal 1004 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=;57aI*Vp~nCK02y>eSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{00G2FL_t(o!`)Xqa>6hW-PM>Gio^wtJ6EYZTtYWdF2Nyu z33L-3fR>UAsBvaU3MT|@8p{J_Wy?0140)9XE&F-m zLeWVn2dS1)!kB^-u{6dQA97-H5lShcv=)ppy_K)E24f7A)<4G5E1Q);@2bI6~$E@Jl;AErC|#F?I^z7 zaDCufi@P7wHAyq4zoe8EX>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#=z00006VoOIv0RI600RN!9r;`8x010qNS#tmY zE+YT{E+YYWr9XB6000McNliru=K=;57XXJ)Xe$5!02y>eSad^gZEa<4bO1wgWnpw> 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/Spacewar.c b/Spacewar.c index c9999d6..2e68620 100644 --- a/Spacewar.c +++ b/Spacewar.c @@ -1,6 +1,7 @@ -// SDL Experiment 09, Barra Ó Catháin. +// SDL Experiment 10, Barra Ó Catháin. // =================================== #include +#include #include #include #include @@ -28,6 +29,15 @@ static inline double normalizeXYVector(xyVector * vector) 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; @@ -102,18 +112,36 @@ int main(int argc, char ** argv) double deltaTime = 0, gravityMagnitude = 0, gravityAcceleration = 0; uint64_t thisFrameTime = SDL_GetPerformanceCounter(), lastFrameTime = 0; bool quit = false, rotatingClockwise = false, rotatingAnticlockwise = false, accelerating = false; - xyVector positionVector = {100, 100}, velocityVector = {0, 0}, gravityVector = {0, 0}, engineVector = {0.1, 0}; - + xyVector positionVector = {100, 100}, velocityVector = {0, 0}, gravityVector = {0, 0}, engineVector = {0.1, 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; + idleTexture = IMG_LoadTexture(renderer, "./10-Ship-Idle.png"); + clockwiseTexture = IMG_LoadTexture(renderer, "./10-Ship-Clockwise.png"); + acceleratingTexture = IMG_LoadTexture(renderer, "./10-Ship-Accelerating.png"); + anticlockwiseTexture = IMG_LoadTexture(renderer, "./10-Ship-Anticlockwise.png"); + + // Enable resizing the window: SDL_SetWindowResizable(window, SDL_TRUE); @@ -230,14 +258,20 @@ int main(int argc, char ** argv) 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); + rotateXYVector(&engineVector, 0.1 * deltaTime); + currentTexture = clockwiseTexture; } if(rotatingAnticlockwise) { - rotateXYVector(&engineVector, -0.1); + rotateXYVector(&engineVector, -0.1 * deltaTime); + currentTexture = anticlockwiseTexture; } // Calculate the new current velocity: @@ -245,6 +279,7 @@ int main(int argc, char ** argv) if(accelerating) { addXYVectorDeltaScaled(&velocityVector, &engineVector, deltaTime); + currentTexture = acceleratingTexture; } // Calculate the new position: @@ -253,17 +288,18 @@ int main(int argc, char ** argv) 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); - // Set the colour to green: - SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); - - // Draw a circle "ship" around the current position: - DrawCircle(renderer, positionX, positionY, 15); + // 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); @@ -276,14 +312,6 @@ int main(int argc, char ** argv) (long)(positionVector.xComponent + velocityVector.xComponent * 15), (long)(positionVector.yComponent + velocityVector.yComponent * 15)); - // Set the colour to blue: - SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); - - // Draw a line showing the direction the engine will accelerate: - SDL_RenderDrawLine(renderer, positionX, positionY, - (long)(positionVector.xComponent + engineVector.xComponent * 300), - (long)(positionVector.yComponent + engineVector.yComponent * 300)); - // Present the rendered graphics: SDL_RenderPresent(renderer); } @@ -291,5 +319,5 @@ int main(int argc, char ** argv) } // =========================================================================================== // Local Variables: -// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-09.c -lm" +// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-10.c -lSDL2_image -lm" // End: