Skip to content

Instantly share code, notes, and snippets.

@aolo2
Last active May 20, 2019 12:14
Show Gist options
  • Select an option

  • Save aolo2/f23059aef86f93ad44644eeae8d484e1 to your computer and use it in GitHub Desktop.

Select an option

Save aolo2/f23059aef86f93ad44644eeae8d484e1 to your computer and use it in GitHub Desktop.
SDL2 pixmap fast drawing skeleton
#include "common.h"
#include <SDL.h>
static const u32 WINDOW_WIDTH = 800;
static const u32 WINDOW_HEIGHT = 600;
static const u32 TARGET_FRAMERATE = 60;
static const f32 TARGET_FRAMETIME = 1000.0f / (f32) TARGET_FRAMERATE;
struct framebuffer {
void *pixels;
s32 width;
s32 height;
s32 pitch;
s32 size;
u8 bpp;
u8 shift_red;
u8 shift_green;
u8 shift_blue;
u8 shift_alpha;
};
#include "matrix.c"
#include "draw.c"
s32
main(void) {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture = NULL;
SDL_Event event;
struct framebuffer fb;
fb.width = 800;
fb.height = 600;
fb.bpp = 4;
fb.pitch = fb.width * fb.bpp;
fb.size = fb.width * fb.height * fb.bpp;
fb.shift_alpha = 24;
fb.shift_red = 16;
fb.shift_green = 8;
fb.shift_blue = 0;
posix_memalign(&fb.pixels, sizeof(void *) * 4, fb.size);
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(WINDOW_WIDTH,
WINDOW_HEIGHT,
0,
&window,
&renderer);
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
fb.width,
fb.height);
struct timespec frametime_beg;
struct timespec frametime_end;
s32 running = 1;
u32 fn = 0;
f32 translate[9];
f32 scale[9];
f32 rotate[9];
f32 transform_final[9];
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = 0;
}
}
clock_gettime(CLOCK_MONOTONIC, &frametime_beg);
// =======RENDER FROM HERE==========
SDL_LockTexture(texture, NULL, &fb.pixels, &fb.pitch);
memset(fb.pixels, 0x00, fb.size);
// lines
bresenham_line(&fb, 0, 0, fb.width - 1, 0);
bresenham_line(&fb, fb.width - 1, 0, fb.width - 1, fb.height - 1);
bresenham_line(&fb, fb.width - 1, fb.height - 1, 0, fb.height - 1);
bresenham_line(&fb, 0, fb.height - 1, 0, 0);
bresenham_line(&fb, 0, 0, fb.width - 1, fb.height - 1);
bresenham_line(&fb, fb.width - 1, 0, 0, fb.height - 1);
// circle
bresenham_circle(&fb, fb.width / 2, fb.height / 2, fabsf(sinf((f32) fn / 100)) * 100);
// triangle
v3 t0 = { 200, 100, 1 };
v3 t1 = { 320, 100, 1 };
v3 t2 = { 120, 200, 1 };
v2 rotation_center = {
(t0.x + t1.x + t2.x) / 3.0f,
(t0.y + t1.y + t2.y) / 3.0f,
};
v2 translate_vector = {
fabsf(cosf((f32) fn / 100)) * 200.0f,
fabsf(cosf((f32) fn / 100)) * 200.0f};
v2 scale_factor = {
fabsf(sinf((f32) fn / 100)) * 2.0f,
fabsf(sinf((f32) fn / 100)) * 2.0f
};
//m3_scale(scale, scale_factor);
m3_translate(translate, translate_vector);
m3_rotate(rotate, rotation_center, (f32) fn / 10);
m3_mul(transform_final, translate, rotate);
t0 = m3_apply(transform_final, t0);
t1 = m3_apply(transform_final, t1);
t2 = m3_apply(transform_final, t2);
fill_triangle(&fb, t0.x, t0.y, t1.x, t1.y, t2.x, t2.y);
SDL_UnlockTexture(texture);
// ============TO HERE=============
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
clock_gettime(CLOCK_MONOTONIC, &frametime_end);
s64 frametime_nsec = (frametime_end.tv_nsec + frametime_end.tv_sec * 1000000000)
- (frametime_beg.tv_nsec + frametime_beg.tv_sec * 1000000000);
s64 frametime_msec = frametime_nsec / 1000000;
s32 frametime_delta = TARGET_FRAMETIME - frametime_msec;
if (frametime_delta > 0) {
SDL_Delay((u32) frametime_delta);
}
++fn;
}
SDL_DestroyRenderer(renderer);
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