Minor reorganization and added SDL2 Experiment 12
- Experiment 12 has a moving camera and wider playfield. Getting pretty cool now.
This commit is contained in:
@ -0,0 +1,260 @@
|
||||
// SDL Experiment 04, Barra Ó Catháin.
|
||||
// ===================================
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
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());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
// Begin accelerating in the direction that is pressed:
|
||||
case SDL_KEYDOWN:
|
||||
{
|
||||
switch (event.key.keysym.sym)
|
||||
{
|
||||
case SDLK_LEFT:
|
||||
{
|
||||
accelLeft = true;
|
||||
break;
|
||||
}
|
||||
case SDLK_RIGHT:
|
||||
{
|
||||
accelRight = true;
|
||||
break;
|
||||
}
|
||||
case SDLK_UP:
|
||||
{
|
||||
accelUp = true;
|
||||
break;
|
||||
}
|
||||
case SDLK_DOWN:
|
||||
{
|
||||
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:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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(velocityX < -10)
|
||||
{
|
||||
velocityX = -10;
|
||||
}
|
||||
|
||||
|
||||
// Deccelerate:
|
||||
if(velocityX != 0)
|
||||
{
|
||||
velocityX = (velocityX < 0) ? velocityX + 1 : velocityX - 1;
|
||||
}
|
||||
|
||||
if(positionY < 640)
|
||||
{
|
||||
velocityY += 1;
|
||||
}
|
||||
if(positionY >= 640 && velocityY !=0 && !accelUp)
|
||||
{
|
||||
velocityY *= -1;
|
||||
velocityY += 1;
|
||||
}
|
||||
|
||||
|
||||
// Move the position:
|
||||
if(velocityX != 0)
|
||||
{
|
||||
positionX += velocityX;
|
||||
if(positionX < 0)
|
||||
{
|
||||
positionX = 640;
|
||||
}
|
||||
if(positionX > 640)
|
||||
{
|
||||
positionX = 0;
|
||||
}
|
||||
}
|
||||
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);
|
||||
|
||||
// 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 circle around the position pointer:
|
||||
DrawCircle(renderer, positionX, positionY, 15);
|
||||
|
||||
// Present the rendered graphics:
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Delay enough so that we run at 144 frames:
|
||||
SDL_Delay(1000 / 60);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// ===========================================================================================
|
||||
// Local Variables:
|
||||
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-04.c -lm"
|
||||
// End:
|
@ -0,0 +1,181 @@
|
||||
// SDL Experiment 05, Barra Ó Catháin.
|
||||
// ===================================
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
bool quit = false;
|
||||
int width = 0, height = 0;
|
||||
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
|
||||
double posX = 0, posY = 0;
|
||||
long positionX = 0, positionY = 0;
|
||||
double velocityX = 20, velocityY = 0, gravityX = 0, gravityY = 0, gravityMagnitude = 0, gravityAcceleration = 0;
|
||||
|
||||
// Initialize the SDL library, video, sound, and input:
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||
{
|
||||
printf("SDL Initialization Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Store the window's current width and height:
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
|
||||
// Calculate the gravity vector:
|
||||
// Calculate the vector between the star and ship:
|
||||
gravityX = (width/2 - posX);
|
||||
gravityY = (height/2 - posY);
|
||||
|
||||
// Make it into a unit vector:
|
||||
gravityMagnitude = sqrt(pow(gravityX, 2) + pow(gravityY, 2));
|
||||
gravityX /= gravityMagnitude;
|
||||
gravityY /= gravityMagnitude;
|
||||
|
||||
// Calculate the gravity between them and scale the vector:
|
||||
if(gravityMagnitude > 15)
|
||||
{
|
||||
gravityAcceleration = 2 * (2500 / pow(gravityMagnitude, 2));
|
||||
gravityX *= gravityAcceleration;
|
||||
gravityY *= gravityAcceleration;
|
||||
}
|
||||
else
|
||||
{
|
||||
gravityAcceleration = 0.02 * (2500 / pow(gravityMagnitude, 2));
|
||||
gravityX *= gravityAcceleration;
|
||||
gravityY *= gravityAcceleration;
|
||||
}
|
||||
|
||||
// Wrap the position if the ship goes off-screen:
|
||||
if(posX > width + 15)
|
||||
{
|
||||
posX = 0;
|
||||
velocityX *= 0.6;
|
||||
}
|
||||
if(posY > height + 15)
|
||||
{
|
||||
posY = 0;
|
||||
velocityY *= 0.6;
|
||||
}
|
||||
if(posX < -15)
|
||||
{
|
||||
posX = width;
|
||||
velocityX *= 0.6;
|
||||
}
|
||||
if(posY < -15)
|
||||
{
|
||||
posY = height;
|
||||
velocityY *= 0.6;
|
||||
}
|
||||
|
||||
// Calculate the new current velocity:
|
||||
velocityX += gravityX;
|
||||
velocityY += gravityY;
|
||||
|
||||
// Calculate the new position:
|
||||
posX += velocityX;
|
||||
posY += velocityY;
|
||||
|
||||
positionX = (long)posX;
|
||||
positionY = (long)posY;
|
||||
|
||||
// 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);
|
||||
|
||||
// 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)((posX + velocityX * 3)), (long)((posY + velocityY * 3)));
|
||||
|
||||
// Present the rendered graphics:
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Delay enough so that we run at 60 frames:
|
||||
SDL_Delay(1000 / 60);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// ===========================================================================================
|
||||
// Local Variables:
|
||||
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-05.c -lm"
|
||||
// End:
|
@ -0,0 +1,183 @@
|
||||
// SDL Experiment 06, Barra Ó Catháin.
|
||||
// ===================================
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
bool quit = false;
|
||||
int width = 0, height = 0;
|
||||
uint64_t thisFrameTime = SDL_GetPerformanceCounter(), lastFrameTime = 0;
|
||||
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
|
||||
double posX = 0, posY = 0, deltaTime = 0;
|
||||
long positionX = 0, positionY = 0;
|
||||
double velocityX = 20, velocityY = 0, gravityX = 0, gravityY = 0, gravityMagnitude = 0, gravityAcceleration = 0;
|
||||
|
||||
// Initialize the SDL library, video, sound, and input:
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||
{
|
||||
printf("SDL Initialization Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Store the window's current width and height:
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
|
||||
// Calculate the gravity vector:
|
||||
// Calculate the vector between the star and ship:
|
||||
gravityX = (width/2 - posX);
|
||||
gravityY = (height/2 - posY);
|
||||
|
||||
// Make it into a unit vector:
|
||||
gravityMagnitude = sqrt(pow(gravityX, 2) + pow(gravityY, 2));
|
||||
gravityX /= gravityMagnitude;
|
||||
gravityY /= gravityMagnitude;
|
||||
|
||||
// Calculate the gravity between them and scale the vector:
|
||||
if(gravityMagnitude > 15)
|
||||
{
|
||||
gravityAcceleration = 2 * (2500 / pow(gravityMagnitude, 2));
|
||||
gravityX *= gravityAcceleration;
|
||||
gravityY *= gravityAcceleration;
|
||||
}
|
||||
else
|
||||
{
|
||||
gravityAcceleration = 0.02 * (2500 / pow(gravityMagnitude, 2));
|
||||
gravityX *= gravityAcceleration;
|
||||
gravityY *= gravityAcceleration;
|
||||
}
|
||||
|
||||
// Wrap the position if the ship goes off-screen:
|
||||
if(posX > width + 15)
|
||||
{
|
||||
posX = 0;
|
||||
velocityX *= 0.6;
|
||||
}
|
||||
if(posY > height + 15)
|
||||
{
|
||||
posY = 0;
|
||||
velocityY *= 0.6;
|
||||
}
|
||||
if(posX < -15)
|
||||
{
|
||||
posX = width;
|
||||
velocityX *= 0.6;
|
||||
}
|
||||
if(posY < -15)
|
||||
{
|
||||
posY = height;
|
||||
velocityY *= 0.6;
|
||||
}
|
||||
|
||||
// Calculate the new current velocity:
|
||||
velocityX += gravityX * (deltaTime * 0.001) * 60;
|
||||
velocityY += gravityY * (deltaTime * 0.001) * 60;
|
||||
|
||||
// Calculate the new position:
|
||||
posX += velocityX * (deltaTime * 0.001) * 60;
|
||||
posY += velocityY * (deltaTime * 0.001) * 60;
|
||||
|
||||
positionX = (long)posX;
|
||||
positionY = (long)posY;
|
||||
|
||||
// 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);
|
||||
|
||||
// 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)(posX + velocityX * 3), (long)(posY + velocityY * 3));
|
||||
|
||||
// Present the rendered graphics:
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// ===========================================================================================
|
||||
// Local Variables:
|
||||
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-06.c -lm"
|
||||
// End:
|
@ -0,0 +1,215 @@
|
||||
// SDL Experiment 07, Barra Ó Catháin.
|
||||
// ===================================
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
bool quit = false;
|
||||
int width = 0, height = 0;
|
||||
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
|
||||
double deltaTime = 0, gravityMagnitude = 0, gravityAcceleration = 0;
|
||||
uint64_t thisFrameTime = SDL_GetPerformanceCounter(), lastFrameTime = 0;
|
||||
long positionX = 0, positionY = 0;
|
||||
xyVector positionVector = {100, 100}, velocityVector = {20, 0}, gravityVector = {0, 0};
|
||||
|
||||
// Initialize the SDL library, video, sound, and input:
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||
{
|
||||
printf("SDL Initialization Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
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 > 15) ?
|
||||
2 * (2500 / pow(gravityMagnitude, 2)):
|
||||
0.02 * (2500 / pow(gravityMagnitude, 2));
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Calculate the new current velocity:
|
||||
addXYVectorDeltaScaled(&velocityVector, &gravityVector, deltaTime);
|
||||
|
||||
// Calculate the new position:
|
||||
addXYVectorDeltaScaled(&positionVector, &velocityVector, deltaTime);
|
||||
|
||||
positionX = (long)positionVector.xComponent;
|
||||
positionY = (long)positionVector.yComponent;
|
||||
|
||||
// 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);
|
||||
|
||||
// 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 * 3),
|
||||
(long)(positionVector.yComponent + velocityVector.yComponent * 3));
|
||||
|
||||
// Present the rendered graphics:
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// ===========================================================================================
|
||||
// Local Variables:
|
||||
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-07.c -lm"
|
||||
// End:
|
@ -0,0 +1,227 @@
|
||||
// SDL Experiment 08, Barra Ó Catháin.
|
||||
// ===================================
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
bool quit = false;
|
||||
long positionX = 0, positionY = 0;
|
||||
int width = 0, height = 0, mouseX = 0, mouseY = 0;
|
||||
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
|
||||
double deltaTime = 0, gravityMagnitude = 0, gravityAcceleration = 0;
|
||||
uint64_t thisFrameTime = SDL_GetPerformanceCounter(), lastFrameTime = 0;
|
||||
xyVector positionVector = {100, 100}, velocityVector = {20, 0}, gravityVector = {0, 0};
|
||||
|
||||
// Initialize the SDL library, video, sound, and input:
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||
{
|
||||
printf("SDL Initialization Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Store the window's current width and height:
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
|
||||
// Store the mouse pointer's current position in the window:
|
||||
SDL_GetMouseState(&mouseX, &mouseY);
|
||||
|
||||
// Calculate the vector between the star and ship:
|
||||
xyVectorBetweenPoints(positionVector.xComponent, positionVector.yComponent, mouseX, mouseY, &gravityVector);
|
||||
|
||||
// Make it into a unit vector:
|
||||
gravityMagnitude = normalizeXYVector(&gravityVector);
|
||||
|
||||
// Calculate the gravity between the star and ship:
|
||||
if(gravityMagnitude > 45)
|
||||
{
|
||||
gravityAcceleration = 2 * (2500 / pow(gravityMagnitude, 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
gravityAcceleration = 0;
|
||||
multiplyXYVector(&velocityVector, -0.98);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Calculate the new current velocity:
|
||||
addXYVectorDeltaScaled(&velocityVector, &gravityVector, deltaTime);
|
||||
|
||||
// Calculate the new position:
|
||||
addXYVectorDeltaScaled(&positionVector, &velocityVector, deltaTime);
|
||||
|
||||
positionX = (long)positionVector.xComponent;
|
||||
positionY = (long)positionVector.yComponent;
|
||||
|
||||
// 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);
|
||||
|
||||
// Set the colour to yellow:
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
|
||||
|
||||
// Draw a circle in the center as the star:
|
||||
DrawCircle(renderer, mouseX, mouseY, 30);
|
||||
|
||||
// Draw a line representing the velocity:
|
||||
SDL_RenderDrawLine(renderer, positionX, positionY,
|
||||
(long)(positionVector.xComponent + velocityVector.xComponent * 3),
|
||||
(long)(positionVector.yComponent + velocityVector.yComponent * 3));
|
||||
|
||||
// Present the rendered graphics:
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Delay enough so that we run at 60 frames:
|
||||
SDL_Delay(1000 / 144);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// ===========================================================================================
|
||||
// Local Variables:
|
||||
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-08.c -lm"
|
||||
// End:
|
@ -0,0 +1,295 @@
|
||||
// SDL Experiment 09, Barra Ó Catháin.
|
||||
// ===================================
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
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};
|
||||
|
||||
// Initialize the SDL library, video, sound, and input:
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||
{
|
||||
printf("SDL Initialization Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// 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;
|
||||
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) ?
|
||||
0.5 * (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;
|
||||
}
|
||||
// Rotate the engine vector if needed:
|
||||
if(rotatingClockwise)
|
||||
{
|
||||
rotateXYVector(&engineVector, 0.1);
|
||||
}
|
||||
if(rotatingAnticlockwise)
|
||||
{
|
||||
rotateXYVector(&engineVector, -0.1);
|
||||
}
|
||||
|
||||
// Calculate the new current velocity:
|
||||
addXYVectorDeltaScaled(&velocityVector, &gravityVector, deltaTime);
|
||||
if(accelerating)
|
||||
{
|
||||
addXYVectorDeltaScaled(&velocityVector, &engineVector, deltaTime);
|
||||
}
|
||||
|
||||
// Calculate the new position:
|
||||
addXYVectorDeltaScaled(&positionVector, &velocityVector, deltaTime);
|
||||
|
||||
positionX = (long)positionVector.xComponent;
|
||||
positionY = (long)positionVector.yComponent;
|
||||
|
||||
// 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);
|
||||
|
||||
// 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));
|
||||
|
||||
// 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);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// ===========================================================================================
|
||||
// Local Variables:
|
||||
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-09.c -lm"
|
||||
// End:
|
Reference in New Issue
Block a user