diff --git a/CMakeLists.txt b/CMakeLists.txt index 508dd3f..67abce6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,12 @@ project(erc) include(sources.cmake) if(APPLE) - set(opengl_library /System/Library/Frameworks/OpenGL.framework) + set(sdl_library /Library/Frameworks/SDL2.framework) + set(sdl_headers /Library/Frameworks/SDL2.framework/Headers) endif() -if(NOT opengl_library) - message(FATAL_ERROR "This CMake file is not yet educated as to where OpenGL resides on your platform. Sorry!") +if(NOT sdl_library) + message(FATAL_ERROR "This CMake file is not yet educated as to where SDL2 resides on your platform. Sorry!") endif() foreach(src ${erc_sources}) @@ -19,7 +20,7 @@ foreach(src ${erc_sources}) endforeach(src) # our header files -include_directories(include /usr/local/include) +include_directories(include /usr/local/include ${sdl_headers}) link_directories(/usr/local/lib) @@ -27,4 +28,4 @@ link_directories(/usr/local/lib) add_executable(erc ${sources} src/main.c) # Graphics -target_link_libraries(erc) +target_link_libraries(erc ${sdl_library}) diff --git a/include/vm_screen.h b/include/vm_screen.h index 0e38b12..ee38e5c 100644 --- a/include/vm_screen.h +++ b/include/vm_screen.h @@ -2,6 +2,7 @@ #define _VM_SCREEN_H_ #include +#include #define VM_SCREEN_DEFWIDTH 800 #define VM_SCREEN_DEFHEIGHT 600 @@ -15,13 +16,14 @@ vm_screen_draw_rect(screen, xpos, ypos, 1, 1) typedef struct { - /* - * These form the components of an RGBA composite color. - */ - int color_red; - int color_green; - int color_blue; - int color_alpha; + SDL_Window *window; + SDL_Renderer *render; + SDL_Rect rect; + + int xcoords; + int ycoords; + + int scale; } vm_screen; extern int vm_screen_add_window(vm_screen *); @@ -32,6 +34,6 @@ extern bool vm_screen_active(vm_screen *); extern void vm_screen_draw_rect(vm_screen *, int, int, int, int); extern void vm_screen_free(vm_screen *); extern vm_screen *vm_screen_create(); -extern void vm_screen_set_color(vm_screen *, int, int, int, int); +extern void vm_screen_set_color(vm_screen *, uint8_t, uint8_t, uint8_t, uint8_t); #endif diff --git a/src/main.c b/src/main.c index 24e3647..a0ee2cc 100644 --- a/src/main.c +++ b/src/main.c @@ -45,7 +45,10 @@ init(int argc, char **argv) // We're literally using stdout in this heavy phase of development. log_open(stdout); - vm_screen_init(); + if (vm_screen_init() != OK) { + fprintf(stderr, "Couldn't initialize video\n"); + exit(1); + } } /* @@ -94,7 +97,7 @@ main(int argc, char **argv) exit(1); } - screen = vm_screen_create(); + screen = vm_screen_create(320, 240, 2); if (screen == NULL) { fprintf(stderr, "Screen creation failed!\n"); exit(1); @@ -106,6 +109,8 @@ main(int argc, char **argv) } while (vm_screen_active(screen)) { + vm_screen_set_color(screen, 255, 0, 0, 255); + vm_screen_draw_rect(screen, 50, 50, 20, 20); vm_screen_refresh(screen); } diff --git a/src/vm_screen.c b/src/vm_screen.c index 5c14476..c9a1c29 100644 --- a/src/vm_screen.c +++ b/src/vm_screen.c @@ -15,19 +15,25 @@ int vm_screen_init() { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + log_critical("Could not initialize video: %s", SDL_GetError()); + return ERR_GFXINIT; + } + return OK; } void vm_screen_finish() { + SDL_Quit(); } /* * Return a new screen. We also set the color to black. */ vm_screen * -vm_screen_create() +vm_screen_create(int xcoords, int ycoords, int scale) { vm_screen *screen; @@ -37,13 +43,35 @@ vm_screen_create() exit(1); } - vm_screen_set_color(screen, 0, 0, 0, 0); + screen->xcoords = xcoords; + screen->ycoords = ycoords; + screen->scale = scale; + + screen->window = NULL; + screen->render = NULL; + screen->rect.x = 0; + screen->rect.y = 0; + screen->rect.w = 0; + screen->rect.h = 0; + return screen; } int vm_screen_add_window(vm_screen *screen) { + SDL_CreateWindowAndRenderer(screen->xcoords * screen->scale, + screen->ycoords * screen->scale, + 0, &screen->window, &screen->render); + + if (screen->window == NULL || screen->render == NULL) { + log_critical("Could not create window: %s", SDL_GetError()); + return ERR_GFXINIT; + } + + vm_screen_set_color(screen, 0, 0, 0, 0); + SDL_RenderClear(screen->render); + return OK; } @@ -53,18 +81,28 @@ vm_screen_add_window(vm_screen *screen) void vm_screen_free(vm_screen *screen) { + SDL_DestroyRenderer(screen->render); + SDL_DestroyWindow(screen->window); free(screen); } bool vm_screen_active(vm_screen *screen) { + static int counter = 5; + + if (counter--) { + return true; + } + return false; } void vm_screen_refresh(vm_screen *screen) { + SDL_RenderPresent(screen->render); + SDL_Delay(2000); } /* @@ -72,15 +110,12 @@ vm_screen_refresh(vm_screen *screen) */ void vm_screen_set_color(vm_screen *screen, - int red, - int green, - int blue, - int alpha) + uint8_t red, + uint8_t green, + uint8_t blue, + uint8_t alpha) { - screen->color_red = red; - screen->color_green = green; - screen->color_blue = blue; - screen->color_alpha = alpha; + SDL_SetRenderDrawColor(screen->render, red, green, blue, alpha); } /* @@ -94,5 +129,13 @@ vm_screen_draw_rect(vm_screen *screen, int xsize, int ysize) { - // FIXME: NOOP + // We need to scale the position and sizes, since the presumption is + // that we are plotting based on the literal x and y coordinates in + // the screen. + screen->rect.x = xpos * screen->scale; + screen->rect.y = ypos * screen->scale; + screen->rect.w = xsize * screen->scale; + screen->rect.h = ysize * screen->scale; + + SDL_RenderFillRect(screen->render, &screen->rect); }