# HG changeset patch # User Michael Pavone # Date 1459139494 25200 # Node ID c8a0dbd7752c4a48c93dd2156c1bb3a3c0a99446 # Parent 769120da2c1f7a4180b3aedd1e259cf7937443ae Forgot to commit SDL code diff -r 769120da2c1f -r c8a0dbd7752c src/system.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/system.h Sun Mar 27 21:31:34 2016 -0700 @@ -0,0 +1,8 @@ +#ifndef SYSTEM_H_ +#define SYSTEM_H_ + +int system_init(int width, int height); +uint16_t *system_get_framebuffer(int *pitch); +void system_framebuffer_updated(); + +#endif //SYSTEM_H_ diff -r 769120da2c1f -r c8a0dbd7752c src/system_sdl.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/system_sdl.c Sun Mar 27 21:31:34 2016 -0700 @@ -0,0 +1,51 @@ +#include +#include + + +SDL_Window *window; +SDL_Renderer *renderer; +SDL_Texture *texture; + +int system_init(int width, int height) +{ + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "Failed to init SDL: %s\n", SDL_GetError()); + return 0; + } + atexit(SDL_Quit); + SDL_CreateWindowAndRenderer(width, height, 0, &window, &renderer); + + if (!window || !renderer) { + fprintf(stderr, "Failed to create window or renderer: %s\n", SDL_GetError()); + return 0; + } + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB444, SDL_TEXTUREACCESS_STREAMING, 320, 240); + if (!texture) { + fprintf(stderr, "Failed to create texture: %s\n", SDL_GetError()); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + return 0; + } + return 1; +} + +//Should be called once per frame to get a pointer to the output buffer at the start of rendering +uint16_t *system_get_framebuffer(int *pitch) +{ + void *pixels; + if (SDL_LockTexture(texture, NULL, &pixels, pitch) < 0) { + fprintf(stderr, "Failed to lock texture: %s\n", SDL_GetError()); + return NULL; + } + return pixels; +} + +//Should be called once per frame at the competion of rendering +//The pointer returned by system_get_framebuffer should be discarded after calling this function +void system_framebuffer_updated() +{ + SDL_UnlockTexture(texture); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); +} +