Skip to content

Instantly share code, notes, and snippets.

@apathyboy
Created April 18, 2017 22:10
Show Gist options
  • Select an option

  • Save apathyboy/ad0343e314e9b4619b8fac17d0217073 to your computer and use it in GitHub Desktop.

Select an option

Save apathyboy/ad0343e314e9b4619b8fac17d0217073 to your computer and use it in GitHub Desktop.
#include <SDL.h>
#include <windows.h>
#include <cstdint>
void Win32_DEBUG_DrawScreenBuffer(void* pixels, int width, int height, int pitch) {
uint8_t* row = reinterpret_cast<uint8_t*>(pixels);
for (int y = 0; y < height; ++y) {
uint32_t* pixel = reinterpret_cast<uint32_t*>(row);
for (int x = 0; x < width; ++x) {
uint8_t blue = static_cast<uint8_t>(x);
uint8_t green = static_cast<uint8_t>(y);
*pixel++ = (green << 16) | blue;
}
row += pitch;
}
}
int CALLBACK WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode) {
SDL_Window* window;
SDL_Surface* screen;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
return 1;
}
window = SDL_CreateWindow("An SDL2 window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
1280, 720, SDL_WINDOW_OPENGL);
if (!window) {
return 1;
}
screen = SDL_GetWindowSurface(window);
if (!screen) {
return 1;
}
bool isRunning = true;
while (isRunning) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
isRunning = false;
}
}
Win32_DEBUG_DrawScreenBuffer(screen->pixels, screen->w, screen->h, screen->pitch);
SDL_UpdateWindowSurface(window);
}
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