Skip to content

Instantly share code, notes, and snippets.

@appblue
Created December 2, 2022 00:08
Show Gist options
  • Select an option

  • Save appblue/52f95af3e073fc5ca206cdccdcc575a0 to your computer and use it in GitHub Desktop.

Select an option

Save appblue/52f95af3e073fc5ca206cdccdcc575a0 to your computer and use it in GitHub Desktop.
SDL2 Animation in ANSI C
// to compile and run: gcc -o Mikas t.c -lsdl2 && ./Mikas
//
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <stdlib.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define RECT_SIZE 64
#define FPS 90
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
/*
* Produces a random int x, min <= x <= max
* following a uniform distribution
*/
int randomInt(int min, int max) {
return min + rand() % abs(max - min + 1);
}
int clamp(int n, int a, int b) {
return MAX(a, MIN(n, b));
}
int main(int argc, char **argv) {
SDL_Window *window;
SDL_Renderer *renderer;
const Uint8 *keyboard;
int renderW;
int renderH;
int frame = 0;
bool quit_flag = false;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return -1;
}
if ((window = SDL_CreateWindow("Mikas Play", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0 /* SDL_WINDOW_FULLSCREEN */ )) == 0)
goto do_exit;
if ((renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC)) == 0)
goto do_exit;
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
SDL_DisplayMode mode;
int res = SDL_GetCurrentDisplayMode(2, &mode);
int real_fps = mode.refresh_rate !=0 ? mode.refresh_rate : 60;
printf("Reported FPS: %d\n", mode.refresh_rate);
/* Come up with a random color */
int r = 128; //randomInt(50, 255);
int g = 128; //randomInt(50, 255);
int b = 128; //randomInt(50, 255);
SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
keyboard = SDL_GetKeyboardState(NULL);
while (!quit_flag) {
frame++;
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit_flag = true;
}
else if (event.type == SDL_KEYDOWN && keyboard[SDL_SCANCODE_Q] != 0) {
quit_flag = true;
}
}
SDL_SetRenderDrawColor(renderer, 0x18, 0x18, 0x18, 0xff);
SDL_RenderClear(renderer);
/* Fill the rectangle in the color */
for(int i=15; i > 0; i-=1) {
SDL_Rect rect;
rect.w = RECT_SIZE;
rect.h = RECT_SIZE;
rect.x = (WINDOW_WIDTH / 2 - 50) * sin(1.0 * 3.14 * (frame-i*4) / FPS) + (WINDOW_WIDTH - RECT_SIZE) / 2;
rect.y = (WINDOW_HEIGHT / 2 - 50) * cos(1.2 * 3.14 * (frame-i*4) / FPS) + (WINDOW_HEIGHT - RECT_SIZE) / 2;
SDL_SetRenderDrawColor(renderer,
clamp(r - i*10, 0, 255),
clamp(g - i*10, 0, 255),
clamp(b - i*10, 0, 255),
clamp(255-i*20, 0, 255));
SDL_RenderFillRect(renderer, &rect);
}
r = clamp(r + randomInt(-1, 1), 0, 255);
g = clamp(g + randomInt(-1, 1), 0, 255);
b = clamp(b + randomInt(-1, 1), 0, 255);
SDL_RenderPresent(renderer);
SDL_Delay(1000 / real_fps);
}
do_exit:
if (renderer != NULL) SDL_DestroyRenderer(renderer);
if (window != NULL) SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment