diff --git a/SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-04.c b/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-04.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-04.c rename to SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-04.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-05.c b/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-05.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-05.c rename to SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-05.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-06.c b/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-06.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-06.c rename to SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-06.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-07.c b/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-07.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-07.c rename to SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-07.c diff --git a/SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-08.c b/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-08.c similarity index 100% rename from SDL2-Bits-And-Pieces/Experiment-04-08-Gravity/SDL2-Experiment-08.c rename to SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-08.c diff --git a/SDL2-Bits-And-Pieces/SDL2-Experiment-09.c b/SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-09.c similarity index 100% rename from SDL2-Bits-And-Pieces/SDL2-Experiment-09.c rename to SDL2-Bits-And-Pieces/Experiment-04-09-Gravity/SDL2-Experiment-09.c diff --git a/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Accelerating-Frame-2.png b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Accelerating-Frame-2.png new file mode 100644 index 0000000..ec49319 Binary files /dev/null and b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Accelerating-Frame-2.png differ diff --git a/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Accelerating.png b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Accelerating.png new file mode 100644 index 0000000..f9b2595 Binary files /dev/null and b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Accelerating.png differ diff --git a/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Anticlockwise.png b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Anticlockwise.png new file mode 100644 index 0000000..22268ea Binary files /dev/null and b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Anticlockwise.png differ diff --git a/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Clockwise.png b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Clockwise.png new file mode 100644 index 0000000..aa9989a Binary files /dev/null and b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Clockwise.png differ diff --git a/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Idle.png b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Idle.png new file mode 100644 index 0000000..b1c6cf6 Binary files /dev/null and b/SDL2-Bits-And-Pieces/Experiment-12/Experiment-12-Images/Ship-Idle.png differ diff --git a/SDL2-Bits-And-Pieces/Experiment-12/SDL2-Experiment-12.c b/SDL2-Bits-And-Pieces/Experiment-12/SDL2-Experiment-12.c new file mode 100644 index 0000000..3c16303 --- /dev/null +++ b/SDL2-Bits-And-Pieces/Experiment-12/SDL2-Experiment-12.c @@ -0,0 +1,353 @@ +// SDL Experiment 12, 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; + uint32_t rendererFlags = SDL_RENDERER_ACCELERATED; + uint64_t thisFrameTime = SDL_GetPerformanceCounter(), lastFrameTime = 0; + long positionX = 512, positionY = 512, starPositionX = 0, starPositionY = 0; + double deltaTime = 0, gravityMagnitude = 0, gravityAcceleration = 0, frameAccumulator = 0; + bool quit = false, rotatingClockwise = false, rotatingAnticlockwise = false, accelerating = false; + xyVector positionVector = {512, 512}, velocityVector = {1, 0}, gravityVector = {0, 0}, engineVector = {0.16, 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, 700, 700, 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, "./Experiment-12-Images/Ship-Idle.png"); + clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-12-Images/Ship-Clockwise.png"); + acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-12-Images/Ship-Accelerating.png"); + anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-12-Images/Ship-Anticlockwise.png"); + acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-12-Images/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; + } + } + } + + // Wrap the position if the ship goes interstellar: + if(positionVector.xComponent > 4096) + { + positionVector.xComponent = -2000; + velocityVector.xComponent *= 0.9; + } + else if(positionVector.xComponent < -4096) + { + positionVector.xComponent = 2000; + velocityVector.xComponent *= 0.9; + } + + if(positionVector.yComponent > 4096) + { + positionVector.yComponent = -2000; + velocityVector.yComponent *= 0.9; + } + else if(positionVector.yComponent < -4096) + { + positionVector.yComponent = 2000; + velocityVector.yComponent *= 0.9; + } + + // 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, starPositionX, starPositionY, &gravityVector); + + // Make it into a unit vector: + gravityMagnitude = normalizeXYVector(&gravityVector); + + // Calculate the gravity between the star and ship: + if(gravityMagnitude != 0) + { + if(gravityMagnitude >= 215) + { + gravityAcceleration = 10 * (9000 / (pow(gravityMagnitude, 2))); + } + else + { + gravityAcceleration = 0.5 * (5000 / (pow(gravityMagnitude, 2))); + } + } + else + { + gravityAcceleration = 1; + } + + // Scale the vector: + multiplyXYVector(&gravityVector, gravityAcceleration); + + // Set the texture to idle: + currentTexture = idleTexture; + + // Rotate the engine vector if needed: + if(rotatingClockwise) + { + rotateXYVector(&engineVector, 0.25 * deltaTime); + currentTexture = clockwiseTexture; + } + if(rotatingAnticlockwise) + { + rotateXYVector(&engineVector, -0.25 * 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 = (width/2) - 15; + shipRect.y = (height/2) - 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 as the star: + DrawCircle(renderer, (long)(starPositionX - positionX) + width/2, (long)(starPositionY - positionY) + height/2 , 200); + + // Draw a line representing the velocity: + SDL_RenderDrawLine(renderer, width/2, height/2, + (long)((width/2) + velocityVector.xComponent * 15), + (long)((height/2) + velocityVector.yComponent * 15)); + + // Set the colour to blue: + SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); + + // Draw a line representing the direction of the star: + normalizeXYVector(&gravityVector); + multiplyXYVector(&gravityVector, 100); + SDL_RenderDrawLine(renderer, width/2, height/2, + (long)((width/2) + gravityVector.xComponent), + (long)((height/2) + gravityVector.yComponent)); + + // Present the rendered graphics: + SDL_RenderPresent(renderer); + } + return 0; +} +// =========================================================================================== +// Local Variables: +// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-12.c -lSDL2_image -lm" +// End: diff --git a/SDL2-Bits-And-Pieces/SDL2-Notes.org b/SDL2-Bits-And-Pieces/SDL2-Notes.org index 48624ea..b2fc704 100644 --- a/SDL2-Bits-And-Pieces/SDL2-Notes.org +++ b/SDL2-Bits-And-Pieces/SDL2-Notes.org @@ -17,3 +17,5 @@ These experiments are based around developing a gravity simulation. into the Spacewar clone I'm planning on building. - Experiment 08: A modified version of the simulation where the mouse is the "star." Fun to play aronud with. +- Experiment 09: A version of the simulation where the ship can be steered and + accelerated.