#include <SDL3/SDL.h>
#if _WIN32 // PLATFORM_WINDOWS
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define TOTAL_POINTS 550
#define MIN_PIXELS_PER_SECOND 30 // move min 30 pixels per second
#define MAX_PIXELS_PER_SECOND 60 // move max 60 pixels per second
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow) {
if (!SDL_Init(SDL_INIT_VIDEO)) {
// SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
MessageBoxA(0, SDL_GetError(), "Couldn't initialize SDL", MB_OK);
return 3;
}
SDL_Window* window;
SDL_Renderer* renderer;
if (!SDL_CreateWindowAndRenderer("SDL3 Draw random points moving across the screen", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
// SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
MessageBoxA(0, SDL_GetError(), "Couldn't create window and renderer", MB_OK);
return 3;
}
/*
typedef struct SDL_FPoint // X and Y coordinates
{
// top left of the window
float x;
float y;
} SDL_FPoint;
*/
SDL_FPoint points[TOTAL_POINTS];
float point_speeds[TOTAL_POINTS];
// Set up random x, y and distance(speed) for all points
for (int i = 0; i < SDL_arraysize(points); i++) {
points[i].x = SDL_randf() * ((float)WINDOW_WIDTH);
points[i].y = SDL_randf() * ((float)WINDOW_HEIGHT);
point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND));
}
Uint64 last_time = SDL_GetTicks();
SDL_Event event;
while (1) {
SDL_PollEvent(&event);
if (event.type == SDL_EVENT_QUIT) {
break;
}
const Uint64 now = SDL_GetTicks();
// elapsed = seconds since last iteration
const float elapsed = ((float)(now - last_time)) / 1000.0f;
// Move all points every new frame
for (int i = 0; i < SDL_arraysize(points); i++) {
const float distance = elapsed * point_speeds[i];
points[i].x += distance;
points[i].y += distance;
// Check off window size
if ((points[i].x >= WINDOW_WIDTH) || (points[i].y >= WINDOW_HEIGHT)) {
// Set a new place for a point
if (SDL_rand(2)) { // Generate a pseudo-random number less than n for positive n
// Sint32 SDLCALL SDL_rand(Sint32 n)
points[i].x = SDL_randf() * ((float)WINDOW_WIDTH);
points[i].y = 0.0f;
}
else {
points[i].x = 0.0f;
points[i].y = SDL_randf() * ((float)WINDOW_HEIGHT);
}
point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND));
}
}
last_time = now;
// draws over what was drawn before
// Set background color
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); // black
SDL_RenderClear(renderer); // start with a blank canvas
// set point color
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); // white
// Draw all the points at once
SDL_RenderPoints(renderer, points, SDL_arraysize(points)); // draw all the points
SDL_RenderPresent(renderer); // put it all on the screen
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
#endif