diff --git a/CMakeLists.txt b/CMakeLists.txt index 924bc0a3..5fc69037 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ option(DEBUG_SAVE "Re-enable the ability to drag-and-drop save files onto the wi set(BACKEND_RENDERER "SDLTexture" CACHE STRING "Which renderer the game should use: 'OpenGL3' for an OpenGL 3.2 renderer, 'OpenGLES2' for an OpenGL ES 2.0 renderer, 'SDLTexture' for SDL2's hardware-accelerated Texture API, 'SDLSurface' for SDL2's software-rendered Surface API, or 'Software' for a handwritten software renderer") set(BACKEND_AUDIO "SDL2" CACHE STRING "Which audio backend the game should use: 'SDL2' or 'miniaudio'") set(BACKEND_CONTROLLER "SDL2" CACHE STRING "Which controller backend the game should use: 'SDL2' or 'GLFW3'") +set(BACKEND_PLATFORM "SDL2" CACHE STRING "Which platform backend the game should use: 'SDL2' or 'GLFW3'") option(LTO "Enable link-time optimisation" OFF) option(MSVC_LINK_STATIC_RUNTIME "Link the static MSVC runtime library" OFF) @@ -164,7 +165,6 @@ add_executable(CSE2 WIN32 "src/ValueView.cpp" "src/ValueView.h" "src/WindowsWrapper.h" - "src/Backends/Platform/SDL2.cpp" "src/Backends/Audio.h" "src/Backends/Controller.h" "src/Backends/Platform.h" @@ -326,6 +326,20 @@ elseif(BACKEND_CONTROLLER MATCHES "GLFW3") target_sources(CSE2 PRIVATE "src/Backends/Controller/GLFW3.cpp") endif() +if(BACKEND_PLATFORM MATCHES "SDL2") + target_sources(CSE2 PRIVATE "src/Backends/Platform/SDL2.cpp") +elseif(BACKEND_PLATFORM MATCHES "GLFW3") + target_sources(CSE2 PRIVATE "src/Backends/Platform/GLFW3.cpp") +endif() + +if(BACKEND_PLATFORM MATCHES "SDL2" AND BACKEND_RENDERER MATCHES "OpenGL3") + target_sources(CSE2 PRIVATE "src/Backends/Window/SDL2-OpenGL3.cpp") +elseif(BACKEND_PLATFORM MATCHES "GLFW3" AND BACKEND_RENDERER MATCHES "OpenGL3") + target_sources(CSE2 PRIVATE "src/Backends/Window/GLFW3-OpenGL3.cpp") +else() + message(FATAL_ERROR "Invalid BACKEND_PLATFORM/BACKEND_RENDERER combination") +endif() + ########## # Tweaks # @@ -381,32 +395,39 @@ set_target_properties(CSE2 PROPERTIES # Dependencies # ################ -if(NOT FORCE_LOCAL_LIBS) - find_package(SDL2) +if(BACKEND_PLATFORM MATCHES "GLFW3") + find_package(glfw3 REQUIRED) + target_link_libraries(CSE2 PRIVATE glfw) endif() -if(TARGET SDL2::SDL2) - # CMake-generated config (Arch, vcpkg, Raspbian) - message(STATUS "Using system SDL2 (CMake, dynamic)") - target_link_libraries(CSE2 PRIVATE SDL2::SDL2 SDL2::SDL2main) -elseif(TARGET SDL2::SDL2-static) - # CMake-generated config (Arch, vcpkg, Raspbian) - message(STATUS "Using system SDL2 (CMake, static)") - target_link_libraries(CSE2 PRIVATE SDL2::SDL2-static SDL2::SDL2main) -elseif(SDL2_FOUND) - # Autotools-generated config (MSYS2) - message(STATUS "Using system SDL2 (Autotools)") - target_include_directories(CSE2 PRIVATE ${SDL2_INCLUDE_DIRS}) - target_link_libraries(CSE2 PRIVATE ${SDL2_LIBRARIES}) -else() - # Compile it ourselves - message(STATUS "Using local SDL2") - set(SDL_SHARED_ENABLED_BY_DEFAULT OFF) - if(MSVC) - set(LIBC ON) # Needed to prevent possible 'symbol already defined' errors +if(BACKEND_PLATFORM MATCHES "SDL2" OR BACKEND_AUDIO MATCHES "SDL2") + if(NOT FORCE_LOCAL_LIBS) + find_package(SDL2) + endif() + + if(TARGET SDL2::SDL2) + # CMake-generated config (Arch, vcpkg, Raspbian) + message(STATUS "Using system SDL2 (CMake, dynamic)") + target_link_libraries(CSE2 PRIVATE SDL2::SDL2 SDL2::SDL2main) + elseif(TARGET SDL2::SDL2-static) + # CMake-generated config (Arch, vcpkg, Raspbian) + message(STATUS "Using system SDL2 (CMake, static)") + target_link_libraries(CSE2 PRIVATE SDL2::SDL2-static SDL2::SDL2main) + elseif(SDL2_FOUND) + # Autotools-generated config (MSYS2) + message(STATUS "Using system SDL2 (Autotools)") + target_include_directories(CSE2 PRIVATE ${SDL2_INCLUDE_DIRS}) + target_link_libraries(CSE2 PRIVATE ${SDL2_LIBRARIES}) + else() + # Compile it ourselves + message(STATUS "Using local SDL2") + set(SDL_SHARED_ENABLED_BY_DEFAULT OFF) + if(MSVC) + set(LIBC ON) # Needed to prevent possible 'symbol already defined' errors + endif() + add_subdirectory("external/SDL2" EXCLUDE_FROM_ALL) + target_link_libraries(CSE2 PRIVATE SDL2-static SDL2main) endif() - add_subdirectory("external/SDL2" EXCLUDE_FROM_ALL) - target_link_libraries(CSE2 PRIVATE SDL2-static SDL2main) endif() if(NOT FORCE_LOCAL_LIBS) diff --git a/src/Backends/Platform.h b/src/Backends/Platform.h index 6e693047..ee661712 100644 --- a/src/Backends/Platform.h +++ b/src/Backends/Platform.h @@ -5,11 +5,9 @@ extern BOOL bActive; void PlatformBackend_Init(void); - -void PlatformBackend_GetBasePath(char *string_buffer); - +void PlatformBackend_Deinit(void); +BOOL PlatformBackend_GetBasePath(char *string_buffer); BOOL PlatformBackend_SystemTask(void); - void PlatformBackend_ShowMessageBox(const char *title, const char *message); - unsigned long PlatformBackend_GetTicks(void); +void PlatformBackend_Delay(unsigned int ticks); diff --git a/src/Backends/Platform/GLFW3.cpp b/src/Backends/Platform/GLFW3.cpp new file mode 100644 index 00000000..432fdaf6 --- /dev/null +++ b/src/Backends/Platform/GLFW3.cpp @@ -0,0 +1,282 @@ +#include "../Platform.h" + +#include +#include + +#include + +#include "../Rendering.h" + +#include "../../WindowsWrapper.h" + +#include "../../KeyControl.h" +#include "../../Main.h" +#include "../../Organya.h" +#include "../../Profile.h" + +BOOL bActive = TRUE; + +extern GLFWwindow *window; + +void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) +{ + (void)window; + (void)scancode; + (void)mods; + + if (action == GLFW_PRESS) + { + switch (key) + { + case GLFW_KEY_ESCAPE: + gKey |= KEY_ESCAPE; + break; + + case GLFW_KEY_W: + gKey |= KEY_MAP; + break; + + case GLFW_KEY_LEFT: + gKey |= KEY_LEFT; + break; + + case GLFW_KEY_RIGHT: + gKey |= KEY_RIGHT; + break; + + case GLFW_KEY_UP: + gKey |= KEY_UP; + break; + + case GLFW_KEY_DOWN: + gKey |= KEY_DOWN; + break; + + case GLFW_KEY_X: + gKey |= KEY_X; + break; + + case GLFW_KEY_Z: + gKey |= KEY_Z; + break; + + case GLFW_KEY_S: + gKey |= KEY_ARMS; + break; + + case GLFW_KEY_A: + gKey |= KEY_ARMSREV; + break; + + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: + gKey |= KEY_SHIFT; + break; + + case GLFW_KEY_F1: + gKey |= KEY_F1; + break; + + case GLFW_KEY_F2: + gKey |= KEY_F2; + break; + + case GLFW_KEY_Q: + gKey |= KEY_ITEM; + break; + + case GLFW_KEY_COMMA: + gKey |= KEY_ALT_LEFT; + break; + + case GLFW_KEY_PERIOD: + gKey |= KEY_ALT_DOWN; + break; + + case GLFW_KEY_SLASH: + gKey |= KEY_ALT_RIGHT; + break; + + case GLFW_KEY_L: + gKey |= KEY_L; + break; + + case GLFW_KEY_EQUAL: + gKey |= KEY_PLUS; + break; + + case GLFW_KEY_F5: + gbUseJoystick = FALSE; + break; + } + } + else if (action == GLFW_RELEASE) + { + switch (key) + { + case GLFW_KEY_ESCAPE: + gKey &= ~KEY_ESCAPE; + break; + + case GLFW_KEY_W: + gKey &= ~KEY_MAP; + break; + + case GLFW_KEY_LEFT: + gKey &= ~KEY_LEFT; + break; + + case GLFW_KEY_RIGHT: + gKey &= ~KEY_RIGHT; + break; + + case GLFW_KEY_UP: + gKey &= ~KEY_UP; + break; + + case GLFW_KEY_DOWN: + gKey &= ~KEY_DOWN; + break; + + case GLFW_KEY_X: + gKey &= ~KEY_X; + break; + + case GLFW_KEY_Z: + gKey &= ~KEY_Z; + break; + + case GLFW_KEY_S: + gKey &= ~KEY_ARMS; + break; + + case GLFW_KEY_A: + gKey &= ~KEY_ARMSREV; + break; + + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: + gKey &= ~KEY_SHIFT; + break; + + case GLFW_KEY_F1: + gKey &= ~KEY_F1; + break; + + case GLFW_KEY_F2: + gKey &= ~KEY_F2; + break; + + case GLFW_KEY_Q: + gKey &= ~KEY_ITEM; + break; + + case GLFW_KEY_COMMA: + gKey &= ~KEY_ALT_LEFT; + break; + + case GLFW_KEY_PERIOD: + gKey &= ~KEY_ALT_DOWN; + break; + + case GLFW_KEY_SLASH: + gKey &= ~KEY_ALT_RIGHT; + break; + + case GLFW_KEY_L: + gKey &= ~KEY_L; + break; + + case GLFW_KEY_EQUAL: + gKey &= ~KEY_PLUS; + break; + } + } +} + +void WindowFocusCallback(GLFWwindow *window, int focused) +{ + (void)window; + + if (focused) + ActiveWindow(); + else + InactiveWindow(); +} + +void WindowSizeCallback(GLFWwindow *window, int width, int height) +{ + (void)window; + (void)width; + (void)height; + + RenderBackend_HandleWindowResize(); +} + +void PlatformBackend_Init(void) +{ + glfwInit(); +} + +void PlatformBackend_Deinit(void) +{ + glfwTerminate(); +} + +BOOL PlatformBackend_GetBasePath(char *string_buffer) +{ + return FALSE; +} + +BOOL PlatformBackend_SystemTask(void) +{ + if (glfwWindowShouldClose(window)) + { + StopOrganyaMusic(); + return FALSE; + } + + while (!bActive) + glfwWaitEvents(); + + glfwPollEvents(); +/* + while (SDL_PollEvent(NULL) || !bActive) + { + SDL_Event event; + + if (!SDL_WaitEvent(&event)) + return FALSE; + + switch (event.type) + { + case SDL_DROPFILE: + LoadProfile(event.drop.file); + SDL_free(event.drop.file); + break; + + + case SDL_RENDER_TARGETS_RESET: + Backend_HandleRenderTargetLoss(); + break; + + } + } +*/ + return TRUE; +} + +void PlatformBackend_ShowMessageBox(const char *title, const char *message) +{ + //SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, message, NULL); +} + +unsigned long PlatformBackend_GetTicks(void) +{ + return (unsigned long)(glfwGetTime() * 1000.0); +} + +void PlatformBackend_Delay(unsigned int ticks) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(ticks)); +} diff --git a/src/Backends/Platform/SDL2.cpp b/src/Backends/Platform/SDL2.cpp index 702b5d16..82bd6211 100644 --- a/src/Backends/Platform/SDL2.cpp +++ b/src/Backends/Platform/SDL2.cpp @@ -2,7 +2,7 @@ #include "SDL.h" -#include "../Rendering.h" +#include "../Window.h" #include "../../WindowsWrapper.h" @@ -25,13 +25,20 @@ void PlatformBackend_Init(void) SDL_InitSubSystem(SDL_INIT_VIDEO); } -void PlatformBackend_GetBasePath(char *string_buffer) +void PlatformBackend_Deinit(void) +{ + SDL_Quit(); +} + +BOOL PlatformBackend_GetBasePath(char *string_buffer) { char *base_path = SDL_GetBasePath(); size_t base_path_length = strlen(base_path); base_path[base_path_length - 1] = '\0'; strcpy(string_buffer, base_path); SDL_free(base_path); + + return TRUE; } BOOL PlatformBackend_SystemTask(void) @@ -262,3 +269,8 @@ unsigned long PlatformBackend_GetTicks(void) { return SDL_GetTicks(); } + +void PlatformBackend_Delay(unsigned int ticks) +{ + SDL_Delay(ticks); +} diff --git a/src/Backends/Rendering.h b/src/Backends/Rendering.h index 12cfcdf5..6c8ecd55 100644 --- a/src/Backends/Rendering.h +++ b/src/Backends/Rendering.h @@ -1,27 +1,26 @@ #pragma once -#include "SDL.h" - #include "../WindowsWrapper.h" typedef struct Backend_Surface Backend_Surface; typedef struct Backend_Glyph Backend_Glyph; -Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen); -void Backend_Deinit(void); -void Backend_DrawScreen(void); -Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height); -void Backend_FreeSurface(Backend_Surface *surface); -BOOL Backend_IsSurfaceLost(Backend_Surface *surface); -void Backend_RestoreSurface(Backend_Surface *surface); -unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height); -void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height); -void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key); -void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue); -Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch); -void Backend_UnloadGlyph(Backend_Glyph *glyph); -void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels); -void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y); -void Backend_FlushGlyphs(void); -void Backend_HandleRenderTargetLoss(void); -void Backend_HandleWindowResize(void); +Backend_Surface* RenderBackend_Init(int screen_width, int screen_height); +void RenderBackend_Deinit(void); +void RenderBackend_DrawScreen(void); +void RenderBackend_ClearScreen(void); +Backend_Surface* RenderBackend_CreateSurface(unsigned int width, unsigned int height); +void RenderBackend_FreeSurface(Backend_Surface *surface); +BOOL RenderBackend_IsSurfaceLost(Backend_Surface *surface); +void RenderBackend_RestoreSurface(Backend_Surface *surface); +unsigned char* RenderBackend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height); +void RenderBackend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height); +void RenderBackend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key); +void RenderBackend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue); +Backend_Glyph* RenderBackend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch); +void RenderBackend_UnloadGlyph(Backend_Glyph *glyph); +void RenderBackend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels); +void RenderBackend_DrawGlyph(Backend_Glyph *glyph, long x, long y); +void RenderBackend_FlushGlyphs(void); +void RenderBackend_HandleRenderTargetLoss(void); +void RenderBackend_HandleWindowResize(void); diff --git a/src/Backends/Rendering/OpenGL3.cpp b/src/Backends/Rendering/OpenGL3.cpp index 4bdb524c..c0ed58dd 100644 --- a/src/Backends/Rendering/OpenGL3.cpp +++ b/src/Backends/Rendering/OpenGL3.cpp @@ -13,13 +13,13 @@ #include "../../../external/glad/include/glad/glad.h" #endif -#include "SDL.h" - #define SPRITEBATCH_IMPLEMENTATION #include "../../../external/cute_spritebatch.h" #include "../../WindowsWrapper.h" +#include "../Platform.h" +#include "../Window.h" #include "../../Resource.h" #define TOTAL_VBOS 8 @@ -69,9 +69,6 @@ typedef struct VertexBufferSlot Vertex vertices[2][3]; } VertexBufferSlot; -static SDL_Window *window; -static SDL_GLContext context; - static GLuint program_texture; static GLuint program_texture_colour_key; static GLuint program_colour_fill; @@ -275,7 +272,7 @@ static GLuint CompileShader(const char *vertex_shader_source, const char *fragme { char buffer[0x200]; glGetShaderInfoLog(vertex_shader, sizeof(buffer), NULL, buffer); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Vertex shader error", buffer, window); + PlatformBackend_ShowMessageBox("Vertex shader error", buffer); return 0; } @@ -291,7 +288,7 @@ static GLuint CompileShader(const char *vertex_shader_source, const char *fragme { char buffer[0x200]; glGetShaderInfoLog(fragment_shader, sizeof(buffer), NULL, buffer); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fragment shader error", buffer, window); + PlatformBackend_ShowMessageBox("Fragment shader error", buffer); return 0; } @@ -308,7 +305,7 @@ static GLuint CompileShader(const char *vertex_shader_source, const char *fragme { char buffer[0x200]; glGetProgramInfoLog(program_id, sizeof(buffer), NULL, buffer); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Shader linker error", buffer, window); + PlatformBackend_ShowMessageBox("Shader linker error", buffer); return 0; } @@ -516,187 +513,108 @@ static void GlyphBatch_DestroyTexture(SPRITEBATCH_U64 texture_id, void *udata) // Render-backend initialisation // ==================== -Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen) +Backend_Surface* RenderBackend_Init(int screen_width, int screen_height) { - puts("Available SDL2 video drivers:"); + printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR)); + printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER)); + printf("GL_VERSION = %s\n", glGetString(GL_VERSION)); - for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) - puts(SDL_GetVideoDriver(i)); + // Set up blending (only used for font-rendering) + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); + //glEnable(GL_DEBUG_OUTPUT); + //glDebugMessageCallback(MessageCallback, 0); -#ifdef USE_OPENGLES2 - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -#else - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + +#ifndef USE_OPENGLES2 + // Set up Vertex Array Object + glGenVertexArrays(1, &vertex_array_id); + glBindVertexArray(vertex_array_id); #endif - window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screen_width, screen_height, SDL_WINDOW_OPENGL); + // Set up Vertex Buffer Objects + glGenBuffers(TOTAL_VBOS, vertex_buffer_ids); - if (window != NULL) + // Set up the vertex attributes + glEnableVertexAttribArray(ATTRIBUTE_INPUT_VERTEX_COORDINATES); + + // Set up our shaders + program_texture = CompileShader(vertex_shader_texture, fragment_shader_texture); + program_texture_colour_key = CompileShader(vertex_shader_texture, fragment_shader_texture_colour_key); + program_colour_fill = CompileShader(vertex_shader_plain, fragment_shader_colour_fill); + program_glyph = CompileShader(vertex_shader_texture, fragment_shader_glyph); + + if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph != 0) { - #ifndef _WIN32 // On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does) - size_t resource_size; - const unsigned char *resource_data = FindResource("ICON_MINI", "ICON", &resource_size); - SDL_RWops *rwops = SDL_RWFromConstMem(resource_data, resource_size); - SDL_Surface *icon_surface = SDL_LoadBMP_RW(rwops, 1); - SDL_SetWindowIcon(window, icon_surface); - SDL_FreeSurface(icon_surface); + // Get shader uniforms + program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour"); + program_glyph_uniform_colour = glGetUniformLocation(program_glyph, "colour"); + + // Set up framebuffer (used for surface-to-surface blitting) + glGenFramebuffers(1, &framebuffer_id); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id); + + // Set up framebuffer screen texture (used for screen-to-surface blitting) + glGenTextures(1, &framebuffer.texture_id); + glBindTexture(GL_TEXTURE_2D, framebuffer.texture_id); + #ifdef USE_OPENGLES2 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + #else + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + #endif + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + #ifndef USE_OPENGLES2 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); #endif - if (fullscreen) - SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); + framebuffer.width = screen_width; + framebuffer.height = screen_height; - context = SDL_GL_CreateContext(window); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0); + glViewport(0, 0, framebuffer.width, framebuffer.height); - if (context != NULL) - { - if (SDL_GL_MakeCurrent(window, context) == 0) - { - #ifndef USE_OPENGLES2 - if (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) - { - // Check if the platform supports OpenGL 3.2 - if (GLAD_GL_VERSION_3_2) - { - #endif - printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR)); - printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER)); - printf("GL_VERSION = %s\n", glGetString(GL_VERSION)); + // Set-up glyph-batcher + spritebatch_config_t config; + spritebatch_set_default_config(&config); + config.pixel_stride = 1; + config.atlas_width_in_pixels = 256; + config.atlas_height_in_pixels = 256; + config.lonely_buffer_count_till_flush = 4; // Start making atlases immediately + config.batch_callback = GlyphBatch_Draw; + config.get_pixels_callback = GlyphBatch_GetPixels; + config.generate_texture_callback = GlyphBatch_CreateTexture; + config.delete_texture_callback = GlyphBatch_DestroyTexture; + spritebatch_init(&glyph_batcher, &config, NULL); - // Set up blending (only used for font-rendering) - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - //glEnable(GL_DEBUG_OUTPUT); - //glDebugMessageCallback(MessageCallback, 0); - - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - #ifndef USE_OPENGLES2 - // Set up Vertex Array Object - glGenVertexArrays(1, &vertex_array_id); - glBindVertexArray(vertex_array_id); - #endif - - // Set up Vertex Buffer Objects - glGenBuffers(TOTAL_VBOS, vertex_buffer_ids); - - // Set up the vertex attributes - glEnableVertexAttribArray(ATTRIBUTE_INPUT_VERTEX_COORDINATES); - - // Set up our shaders - program_texture = CompileShader(vertex_shader_texture, fragment_shader_texture); - program_texture_colour_key = CompileShader(vertex_shader_texture, fragment_shader_texture_colour_key); - program_colour_fill = CompileShader(vertex_shader_plain, fragment_shader_colour_fill); - program_glyph = CompileShader(vertex_shader_texture, fragment_shader_glyph); - - if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph != 0) - { - // Get shader uniforms - program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour"); - program_glyph_uniform_colour = glGetUniformLocation(program_glyph, "colour"); - - // Set up framebuffer (used for surface-to-surface blitting) - glGenFramebuffers(1, &framebuffer_id); - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id); - - // Set up framebuffer screen texture (used for screen-to-surface blitting) - glGenTextures(1, &framebuffer.texture_id); - glBindTexture(GL_TEXTURE_2D, framebuffer.texture_id); - #ifdef USE_OPENGLES2 - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - #else - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - #endif - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - #ifndef USE_OPENGLES2 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - #endif - - framebuffer.width = screen_width; - framebuffer.height = screen_height; - - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0); - glViewport(0, 0, framebuffer.width, framebuffer.height); - - // Set-up glyph-batcher - spritebatch_config_t config; - spritebatch_set_default_config(&config); - config.pixel_stride = 1; - config.atlas_width_in_pixels = 256; - config.atlas_height_in_pixels = 256; - config.lonely_buffer_count_till_flush = 4; // Start making atlases immediately - config.batch_callback = GlyphBatch_Draw; - config.get_pixels_callback = GlyphBatch_GetPixels; - config.generate_texture_callback = GlyphBatch_CreateTexture; - config.delete_texture_callback = GlyphBatch_DestroyTexture; - spritebatch_init(&glyph_batcher, &config, NULL); - - return &framebuffer; - } - - if (program_glyph != 0) - glDeleteProgram(program_glyph); - - if (program_colour_fill != 0) - glDeleteProgram(program_colour_fill); - - if (program_texture_colour_key != 0) - glDeleteProgram(program_texture_colour_key); - - if (program_texture != 0) - glDeleteProgram(program_texture); - - glDeleteBuffers(TOTAL_VBOS, vertex_buffer_ids); - #ifndef USE_OPENGLES2 - glDeleteVertexArrays(1, &vertex_array_id); - #endif - #ifndef USE_OPENGLES2 - } - else - { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Your system does not support OpenGL 3.2", window); - } - } - else - { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not load OpenGL functions", window); - } - #endif - } - else - { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "SDL_GL_MakeCurrent failed", window); - } - - SDL_GL_DeleteContext(context); - } - else - { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not create OpenGL context", window); - } - - SDL_DestroyWindow(window); - } - else - { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not create window", NULL); + return &framebuffer; } + if (program_glyph != 0) + glDeleteProgram(program_glyph); + + if (program_colour_fill != 0) + glDeleteProgram(program_colour_fill); + + if (program_texture_colour_key != 0) + glDeleteProgram(program_texture_colour_key); + + if (program_texture != 0) + glDeleteProgram(program_texture); + + glDeleteBuffers(TOTAL_VBOS, vertex_buffer_ids); +#ifndef USE_OPENGLES2 + glDeleteVertexArrays(1, &vertex_array_id); +#endif + return NULL; } -void Backend_Deinit(void) +void RenderBackend_Deinit(void) { free(local_vertex_buffer); @@ -712,11 +630,9 @@ void Backend_Deinit(void) #ifndef USE_OPENGLES2 glDeleteVertexArrays(1, &vertex_array_id); #endif - SDL_GL_DeleteContext(context); - SDL_DestroyWindow(window); } -void Backend_DrawScreen(void) +void RenderBackend_DrawScreen(void) { spritebatch_tick(&glyph_batcher); @@ -772,21 +688,22 @@ void Backend_DrawScreen(void) FlushVertexBuffer(); - SDL_GL_SwapWindow(window); + // Switch back to our framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id); +} +void RenderBackend_ClearScreen(void) +{ // According to https://www.khronos.org/opengl/wiki/Common_Mistakes#Swap_Buffers // the buffer should always be cleared, even if it seems unnecessary glClear(GL_COLOR_BUFFER_BIT); - - // Switch back to our framebuffer - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id); } // ==================== // Surface management // ==================== -Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height) +Backend_Surface* RenderBackend_CreateSurface(unsigned int width, unsigned int height) { Backend_Surface *surface = (Backend_Surface*)malloc(sizeof(Backend_Surface)); @@ -816,7 +733,7 @@ Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height) return surface; } -void Backend_FreeSurface(Backend_Surface *surface) +void RenderBackend_FreeSurface(Backend_Surface *surface) { if (surface == NULL) return; @@ -829,19 +746,19 @@ void Backend_FreeSurface(Backend_Surface *surface) free(surface); } -BOOL Backend_IsSurfaceLost(Backend_Surface *surface) +BOOL RenderBackend_IsSurfaceLost(Backend_Surface *surface) { (void)surface; return FALSE; } -void Backend_RestoreSurface(Backend_Surface *surface) +void RenderBackend_RestoreSurface(Backend_Surface *surface) { (void)surface; } -unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height) +unsigned char* RenderBackend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height) { if (surface == NULL) return NULL; @@ -851,7 +768,7 @@ unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch return surface->pixels; } -void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height) +void RenderBackend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height) { if (surface == NULL) return; @@ -871,7 +788,7 @@ void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigne // Drawing // ==================== -void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key) +void RenderBackend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key) { if (source_surface == NULL || destination_surface == NULL) return; @@ -947,7 +864,7 @@ void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Sur vertex_buffer_slot->vertices[1][2].vertex_coordinate.y = vertex_bottom; } -void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue) +void RenderBackend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue) { static unsigned char last_red; static unsigned char last_green; @@ -1012,7 +929,7 @@ void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned cha // Glyph management // ==================== -Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch) +Backend_Glyph* RenderBackend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch) { Backend_Glyph *glyph = (Backend_Glyph*)malloc(sizeof(Backend_Glyph)); @@ -1043,7 +960,7 @@ Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width return NULL; } -void Backend_UnloadGlyph(Backend_Glyph *glyph) +void RenderBackend_UnloadGlyph(Backend_Glyph *glyph) { if (glyph == NULL) return; @@ -1052,19 +969,19 @@ void Backend_UnloadGlyph(Backend_Glyph *glyph) free(glyph); } -void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels) +void RenderBackend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels) { glyph_destination_surface = destination_surface; memcpy(glyph_colour_channels, colour_channels, sizeof(glyph_colour_channels)); } -void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y) +void RenderBackend_DrawGlyph(Backend_Glyph *glyph, long x, long y) { spritebatch_push(&glyph_batcher, (SPRITEBATCH_U64)glyph, glyph->pitch, glyph->height, x, y, 1.0f, 1.0f, 0.0f, 0.0f, 0); } -void Backend_FlushGlyphs(void) +void RenderBackend_FlushGlyphs(void) { spritebatch_defrag(&glyph_batcher); spritebatch_flush(&glyph_batcher); @@ -1074,12 +991,12 @@ void Backend_FlushGlyphs(void) // Misc. // ==================== -void Backend_HandleRenderTargetLoss(void) +void RenderBackend_HandleRenderTargetLoss(void) { // No problem for us } -void Backend_HandleWindowResize(void) +void RenderBackend_HandleWindowResize(void) { // No problem for us } diff --git a/src/Backends/Window.h b/src/Backends/Window.h new file mode 100644 index 00000000..6568e705 --- /dev/null +++ b/src/Backends/Window.h @@ -0,0 +1,25 @@ +#pragma once + +#include "../WindowsWrapper.h" + +#include "Rendering.h" + +Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen); +void Backend_Deinit(void); +void Backend_DrawScreen(void); +void Backend_ClearScreen(void); +Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height); +void Backend_FreeSurface(Backend_Surface *surface); +BOOL Backend_IsSurfaceLost(Backend_Surface *surface); +void Backend_RestoreSurface(Backend_Surface *surface); +unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height); +void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height); +void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key); +void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue); +Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch); +void Backend_UnloadGlyph(Backend_Glyph *glyph); +void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels); +void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y); +void Backend_FlushGlyphs(void); +void Backend_HandleRenderTargetLoss(void); +void Backend_HandleWindowResize(void); diff --git a/src/Backends/Window/GLFW3-OpenGL3.cpp b/src/Backends/Window/GLFW3-OpenGL3.cpp new file mode 100644 index 00000000..82e814d8 --- /dev/null +++ b/src/Backends/Window/GLFW3-OpenGL3.cpp @@ -0,0 +1,181 @@ +#include "../Window.h" + +#ifdef USE_OPENGLES2 +#include +#else +#include "../../../external/glad/include/glad/glad.h" +#endif + +#include + +#include + +#include "../../WindowsWrapper.h" + +#include "../Platform.h" +#include "../../Resource.h" + +// Horrible hacks +GLFWwindow *window; + +void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); +void WindowFocusCallback(GLFWwindow *window, int focused); +void WindowSizeCallback(GLFWwindow *window, int width, int height); + +Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen) +{ +#ifdef USE_OPENGLES2 + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_ES); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); +#else + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); +#endif + + window = glfwCreateWindow(screen_width, screen_height, window_title, NULL, NULL); + + if (window != NULL) + {/* + #ifndef _WIN32 // On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does) + size_t resource_size; + const unsigned char *resource_data = FindResource("ICON_MINI", "ICON", &resource_size); + SDL_RWops *rwops = SDL_RWFromConstMem(resource_data, resource_size); + SDL_Surface *icon_surface = SDL_LoadBMP_RW(rwops, 1); + SDL_SetWindowIcon(window, icon_surface); + SDL_FreeSurface(icon_surface); + #endif +*/ + if (fullscreen) + glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screen_width, screen_height, GLFW_DONT_CARE); + + glfwMakeContextCurrent(window); + + #ifndef USE_OPENGLES2 + if (gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + // Check if the platform supports OpenGL 3.2 + if (GLAD_GL_VERSION_3_2) + { + #endif + glfwSetKeyCallback(window, KeyCallback); + glfwSetWindowFocusCallback(window, WindowFocusCallback); + glfwSetWindowSizeCallback(window, WindowSizeCallback); + + return RenderBackend_Init(screen_width, screen_height); + #ifndef USE_OPENGLES2 + } + else + { + PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Your system does not support OpenGL 3.2"); + } + } + else + { + PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not load OpenGL functions"); + } + #endif + + glfwDestroyWindow(window); + } + else + { + PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not create window"); + } + + return NULL; +} + +void Backend_Deinit(void) +{ + RenderBackend_Deinit(); + + glfwDestroyWindow(window); +} + +void Backend_DrawScreen(void) +{ + RenderBackend_DrawScreen(); + + glfwSwapBuffers(window); + + RenderBackend_ClearScreen(); +} + +Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height) +{ + return RenderBackend_CreateSurface(width, height); + +} + +void Backend_FreeSurface(Backend_Surface *surface) +{ + RenderBackend_FreeSurface(surface); +} + +BOOL Backend_IsSurfaceLost(Backend_Surface *surface) +{ + return RenderBackend_IsSurfaceLost(surface); +} + +void Backend_RestoreSurface(Backend_Surface *surface) +{ + RenderBackend_RestoreSurface(surface); +} + +unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height) +{ + return RenderBackend_LockSurface(surface, pitch, width, height); +} + +void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height) +{ + RenderBackend_UnlockSurface(surface, width, height); +} + +void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key) +{ + RenderBackend_Blit(source_surface, rect, destination_surface, x, y, colour_key); +} + +void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue) +{ + RenderBackend_ColourFill(surface, rect, red, green, blue); +} + +Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch) +{ + return RenderBackend_LoadGlyph(pixels, width, height, pitch); +} + +void Backend_UnloadGlyph(Backend_Glyph *glyph) +{ + RenderBackend_UnloadGlyph(glyph); +} + +void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels) +{ + RenderBackend_PrepareToDrawGlyphs(destination_surface, colour_channels); +} + +void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y) +{ + RenderBackend_DrawGlyph(glyph, x, y); +} + +void Backend_FlushGlyphs(void) +{ + RenderBackend_FlushGlyphs(); +} + +void Backend_HandleRenderTargetLoss(void) +{ + RenderBackend_HandleRenderTargetLoss(); +} + +void Backend_HandleWindowResize(void) +{ + RenderBackend_HandleWindowResize(); +} diff --git a/src/Backends/Window/SDL2-OpenGL3.cpp b/src/Backends/Window/SDL2-OpenGL3.cpp new file mode 100644 index 00000000..25a1de45 --- /dev/null +++ b/src/Backends/Window/SDL2-OpenGL3.cpp @@ -0,0 +1,197 @@ +#include "../Window.h" + +#ifdef USE_OPENGLES2 +#include +#else +#include "../../../external/glad/include/glad/glad.h" +#endif + +#include "SDL.h" + +#include + +#include "../../WindowsWrapper.h" + +#include "../../Resource.h" + +static SDL_Window *window; +static SDL_GLContext context; + +Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen) +{ + puts("Available SDL2 video drivers:"); + + for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) + puts(SDL_GetVideoDriver(i)); + + printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); + +#ifdef USE_OPENGLES2 + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); +#else + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); +#endif + + window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screen_width, screen_height, SDL_WINDOW_OPENGL); + + if (window != NULL) + { + #ifndef _WIN32 // On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does) + size_t resource_size; + const unsigned char *resource_data = FindResource("ICON_MINI", "ICON", &resource_size); + SDL_RWops *rwops = SDL_RWFromConstMem(resource_data, resource_size); + SDL_Surface *icon_surface = SDL_LoadBMP_RW(rwops, 1); + SDL_SetWindowIcon(window, icon_surface); + SDL_FreeSurface(icon_surface); + #endif + + if (fullscreen) + SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); + + context = SDL_GL_CreateContext(window); + + if (context != NULL) + { + if (SDL_GL_MakeCurrent(window, context) == 0) + { + #ifndef USE_OPENGLES2 + if (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) + { + // Check if the platform supports OpenGL 3.2 + if (GLAD_GL_VERSION_3_2) + { + #endif + return RenderBackend_Init(screen_width, screen_height); + #ifndef USE_OPENGLES2 + } + else + { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Your system does not support OpenGL 3.2", window); + } + } + else + { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not load OpenGL functions", window); + } + #endif + } + else + { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "SDL_GL_MakeCurrent failed", window); + } + + SDL_GL_DeleteContext(context); + } + else + { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not create OpenGL context", window); + } + + SDL_DestroyWindow(window); + } + else + { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not create window", NULL); + } + + return NULL; +} + +void Backend_Deinit(void) +{ + RenderBackend_Deinit(); + + SDL_GL_DeleteContext(context); + SDL_DestroyWindow(window); +} + +void Backend_DrawScreen(void) +{ + RenderBackend_DrawScreen(); + + SDL_GL_SwapWindow(window); + + RenderBackend_ClearScreen(); +} + +Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height) +{ + return RenderBackend_CreateSurface(width, height); + +} + +void Backend_FreeSurface(Backend_Surface *surface) +{ + RenderBackend_FreeSurface(surface); +} + +BOOL Backend_IsSurfaceLost(Backend_Surface *surface) +{ + return RenderBackend_IsSurfaceLost(surface); +} + +void Backend_RestoreSurface(Backend_Surface *surface) +{ + RenderBackend_RestoreSurface(surface); +} + +unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height) +{ + return RenderBackend_LockSurface(surface, pitch, width, height); +} + +void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height) +{ + RenderBackend_UnlockSurface(surface, width, height); +} + +void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key) +{ + RenderBackend_Blit(source_surface, rect, destination_surface, x, y, colour_key); +} + +void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue) +{ + RenderBackend_ColourFill(surface, rect, red, green, blue); +} + +Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch) +{ + return RenderBackend_LoadGlyph(pixels, width, height, pitch); +} + +void Backend_UnloadGlyph(Backend_Glyph *glyph) +{ + RenderBackend_UnloadGlyph(glyph); +} + +void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels) +{ + RenderBackend_PrepareToDrawGlyphs(destination_surface, colour_channels); +} + +void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y) +{ + RenderBackend_DrawGlyph(glyph, x, y); +} + +void Backend_FlushGlyphs(void) +{ + RenderBackend_FlushGlyphs(); +} + +void Backend_HandleRenderTargetLoss(void) +{ + RenderBackend_HandleRenderTargetLoss(); +} + +void Backend_HandleWindowResize(void) +{ + RenderBackend_HandleWindowResize(); +} diff --git a/src/Draw.cpp b/src/Draw.cpp index e723afad..4988b326 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -7,7 +7,8 @@ #include "WindowsWrapper.h" -#include "Backends/Rendering.h" +#include "Backends/Platform.h" +#include "Backends/Window.h" #include "Bitmap.h" #include "CommonDefines.h" #include "Ending.h" @@ -58,15 +59,15 @@ BOOL Flip_SystemTask(void) return FALSE; // Framerate limiter - timeNow = SDL_GetTicks(); + timeNow = PlatformBackend_GetTicks(); - if (SDL_TICKS_PASSED(timeNow, timePrev + 20)) + if (timeNow >= timePrev + 20) break; - SDL_Delay(1); + PlatformBackend_Delay(1); } - if (SDL_TICKS_PASSED(timeNow, timePrev + 100)) + if (timeNow >= timePrev + 100) timePrev = timeNow; // If the timer is freakishly out of sync, panic and reset it, instead of spamming frames for who-knows how long else timePrev += 20; diff --git a/src/Font.cpp b/src/Font.cpp index 9d17cbed..c244c5db 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -13,7 +13,7 @@ #include "Draw.h" #include "File.h" -#include "Backends/Rendering.h" +#include "Backends/Window.h" // Cave Story wasn't intended to use font anti-aliasing. It's only because Microsoft enabled it // by default from Windows Vista onwards that the game started using it. diff --git a/src/Main.cpp b/src/Main.cpp index 04555a3c..27b8491b 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -63,11 +63,11 @@ unsigned long GetFramePerSecound(void) if (need_new_base_tick) { - base_tick = SDL_GetTicks(); + base_tick = PlatformBackend_GetTicks(); need_new_base_tick = FALSE; } - current_tick = SDL_GetTicks(); + current_tick = PlatformBackend_GetTicks(); ++current_frame; if (base_tick + 1000 <= current_tick) @@ -91,7 +91,19 @@ int main(int argc, char *argv[]) PlatformBackend_Init(); // Get executable's path - PlatformBackend_GetBasePath(gModulePath); + if (!PlatformBackend_GetBasePath(gModulePath)) + { + strcpy(gModulePath, argv[0]); + + for (size_t i = strlen(gModulePath);; --i) + { + if (i == 0 || gModulePath[i] == '\\' || gModulePath[i] == '/') + { + gModulePath[i] = '\0'; + break; + } + } + } // Get path of the data folder strcpy(gDataPath, gModulePath); @@ -308,6 +320,9 @@ int main(int argc, char *argv[]) SDL_FreeCursor(cursor); SDL_FreeSurface(cursor_surface); */ + + PlatformBackend_Deinit(); + return EXIT_SUCCESS; }