Skip to content

Instantly share code, notes, and snippets.

@Ziemas
Created April 27, 2024 01:08
Show Gist options
  • Save Ziemas/dee0e33091f47dd67846ebb9108be89b to your computer and use it in GitHub Desktop.
Save Ziemas/dee0e33091f47dd67846ebb9108be89b to your computer and use it in GitHub Desktop.
#include "SDL.h"
#include <limits.h>
#include <stdio.h>
#define SCREENWIDTH 640
#define SCREENHEIGHT 480
SDL_Renderer *rend;
SDL_Window *win;
SDL_Surface *screen_surface;
char pixels[SCREENHEIGHT * SCREENWIDTH];
SDL_Colour pal[256];
void
draw()
{
SDL_RenderClear(rend);
for (int i = 0; i < sizeof(pixels); i++) {
pixels[i] = rand() > (INT_MAX / 2) ? 1 : 0;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(rend, screen_surface);
SDL_RenderCopy(rend, texture, NULL, NULL);
SDL_DestroyTexture(texture);
SDL_RenderPresent(rend);
}
int
main()
{
int flags, rflags;
memset(pixels, 0, sizeof(pixels));
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
printf("Failed to init SDL: %s\n", SDL_GetError());
return 1;
}
flags = SDL_WINDOW_RESIZABLE;
win = SDL_CreateWindow("static", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREENWIDTH,
SCREENHEIGHT, flags);
if (!win) {
printf("Failed to create window: %s\n", SDL_GetError());
return 1;
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
rflags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
rend = SDL_CreateRenderer(win, -1, rflags);
if (!rend) {
printf("Failed to create renderer: %s\n", SDL_GetError());
return 1;
}
SDL_GL_SetSwapInterval(1);
screen_surface = SDL_CreateRGBSurfaceFrom(pixels, SCREENWIDTH, SCREENHEIGHT, 8, SCREENWIDTH, 0,
0, 0, 0);
if (!screen_surface) {
printf("Failed to create surface: %s\n", SDL_GetError());
return 1;
}
SDL_RenderSetLogicalSize(rend, SCREENWIDTH, SCREENHEIGHT);
pal[0].a = 0xff;
pal[0].r = 0;
pal[0].g = 0;
pal[0].b = 0;
for (int i = 1; i < 256; i++) {
pal[i].a = 0xff;
pal[i].r = 0xff;
pal[i].g = 0xff;
pal[i].b = 0xff;
}
SDL_SetPaletteColors(screen_surface->format->palette, pal, 0, 256);
for (;;) {
draw();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment