Begin merge of Backend_Init/Backend_CreateWindow

OpenGL3+OpenGLES2 backend done so far
This commit is contained in:
Clownacy 2020-01-22 22:19:55 +00:00
parent 3baba6a727
commit 6dac8254e2
5 changed files with 129 additions and 147 deletions

View file

@ -14,8 +14,7 @@ typedef enum FontPixelMode
typedef struct Backend_Surface Backend_Surface;
typedef struct Backend_Glyph Backend_Glyph;
SDL_Window* Backend_CreateWindow(const char *title, int width, int height);
Backend_Surface* Backend_Init(SDL_Window *window);
Backend_Surface* Backend_Init(const char *title, int width, int height, BOOL fullscreen);
void Backend_Deinit(void);
void Backend_DrawScreen(void);
Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height);

View file

@ -17,6 +17,8 @@
#include "../../WindowsWrapper.h"
#include "../../Resource.h"
#define TOTAL_VBOS 2
#define ATTRIBUTE_INPUT_VERTEX_COORDINATES 1
@ -412,7 +414,7 @@ static void FlushVertexBuffer(void)
current_vertex_buffer_slot = 0;
}
SDL_Window* Backend_CreateWindow(const char *title, int width, int height)
Backend_Surface* Backend_Init(const char *title, int width, int height, BOOL fullscreen)
{
#ifdef USE_OPENGLES2
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
@ -426,126 +428,138 @@ SDL_Window* Backend_CreateWindow(const char *title, int width, int height)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#endif
return SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
}
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
Backend_Surface* Backend_Init(SDL_Window *p_window)
{
window = p_window;
int window_width, window_height;
SDL_GetWindowSize(window, &window_width, &window_height);
context = SDL_GL_CreateContext(window);
if (context != NULL)
if (window != NULL)
{
if (SDL_GL_MakeCurrent(window, context) == 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);
#endif
if (fullscreen)
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
int window_width, window_height;
SDL_GetWindowSize(window, &window_width, &window_height);
context = SDL_GL_CreateContext(window);
if (context != NULL)
{
#ifndef USE_OPENGLES2
if (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
#endif
if (SDL_GL_MakeCurrent(window, context) == 0)
{
#ifndef USE_OPENGLES2
// Check if the platform supports OpenGL 3.2
if (GLAD_GL_VERSION_3_2)
if (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
#endif
{
#ifndef NDEBUG
printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR));
printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER));
printf("GL_VERSION = %s\n", glGetString(GL_VERSION));
#endif
//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);
// Check if the platform supports OpenGL 3.2
if (GLAD_GL_VERSION_3_2)
#endif
// Set up Vertex Buffer Objects
glGenBuffers(TOTAL_VBOS, vertex_buffer_ids);
// Set up the vertex attributes
glEnableVertexAttribArray(1);
// 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_normal = CompileShader(vertex_shader_texture, fragment_shader_glyph_normal);
program_glyph_subpixel_part1 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part1);
program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2);
if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph_normal != 0 && program_glyph_subpixel_part1 != 0 && program_glyph_subpixel_part2 != 0)
{
// Get shader uniforms
program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
program_glyph_normal_uniform_colour = glGetUniformLocation(program_glyph_normal, "colour");
program_glyph_subpixel_part2_uniform_colour = glGetUniformLocation(program_glyph_subpixel_part2, "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, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
#ifndef NDEBUG
printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR));
printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER));
printf("GL_VERSION = %s\n", glGetString(GL_VERSION));
#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);
//glEnable(GL_DEBUG_OUTPUT);
//glDebugMessageCallback(MessageCallback, 0);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
#ifndef USE_OPENGLES2
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// Set up Vertex Array Object
glGenVertexArrays(1, &vertex_array_id);
glBindVertexArray(vertex_array_id);
#endif
framebuffer.width = window_width;
framebuffer.height = window_height;
// Set up Vertex Buffer Objects
glGenBuffers(TOTAL_VBOS, vertex_buffer_ids);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0);
glViewport(0, 0, framebuffer.width, framebuffer.height);
// Set up the vertex attributes
glEnableVertexAttribArray(1);
return &framebuffer;
// 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_normal = CompileShader(vertex_shader_texture, fragment_shader_glyph_normal);
program_glyph_subpixel_part1 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part1);
program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2);
if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph_normal != 0 && program_glyph_subpixel_part1 != 0 && program_glyph_subpixel_part2 != 0)
{
// Get shader uniforms
program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
program_glyph_normal_uniform_colour = glGetUniformLocation(program_glyph_normal, "colour");
program_glyph_subpixel_part2_uniform_colour = glGetUniformLocation(program_glyph_subpixel_part2, "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, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, window_width, window_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 = window_width;
framebuffer.height = window_height;
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0);
glViewport(0, 0, framebuffer.width, framebuffer.height);
return &framebuffer;
}
if (program_glyph_subpixel_part2 != 0)
glDeleteProgram(program_glyph_subpixel_part2);
if (program_glyph_subpixel_part1 != 0)
glDeleteProgram(program_glyph_subpixel_part1);
if (program_glyph_normal != 0)
glDeleteProgram(program_glyph_normal);
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
}
if (program_glyph_subpixel_part2 != 0)
glDeleteProgram(program_glyph_subpixel_part2);
if (program_glyph_subpixel_part1 != 0)
glDeleteProgram(program_glyph_subpixel_part1);
if (program_glyph_normal != 0)
glDeleteProgram(program_glyph_normal);
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
}
}
SDL_GL_DeleteContext(context);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
}
return NULL;
@ -568,6 +582,7 @@ void Backend_Deinit(void)
glDeleteVertexArrays(1, &vertex_array_id);
#endif
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
}
void Backend_DrawScreen(void)

View file

@ -84,7 +84,7 @@ BOOL Flip_SystemTask(void)
return TRUE;
}
SDL_Window* CreateWindow(const char *title, int width, int height)
BOOL StartDirectDraw(const char *title, int width, int height, int lMagnification)
{
#ifndef NDEBUG
puts("Available SDL2 video drivers:");
@ -95,11 +95,6 @@ SDL_Window* CreateWindow(const char *title, int width, int height)
printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver());
#endif
return Backend_CreateWindow(title, width, height);
}
BOOL StartDirectDraw(SDL_Window *window, int lMagnification)
{
memset(surface_metadata, 0, sizeof(surface_metadata));
switch (lMagnification)
@ -117,11 +112,10 @@ BOOL StartDirectDraw(SDL_Window *window, int lMagnification)
case 2:
magnification = 2;
fullscreen = TRUE;
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
break;
}
framebuffer = Backend_Init(window);
framebuffer = Backend_Init(title, width, height, fullscreen);
if (framebuffer == NULL)
return FALSE;

View file

@ -51,8 +51,7 @@ typedef enum SurfaceID
} SurfaceID;
BOOL Flip_SystemTask(void);
SDL_Window* CreateWindow(const char *title, int width, int height);
BOOL StartDirectDraw(SDL_Window *window, int lMagnification);
BOOL StartDirectDraw(const char *title, int width, int height, int lMagnification);
void EndDirectDraw(void);
void ReleaseSurface(SurfaceID s);
BOOL MakeSurface_Resource(const char *name, SurfaceID surf_no);

View file

@ -193,15 +193,13 @@ int main(int argc, char *argv[])
RECT unused_rect = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
#ifdef _WIN32
#ifdef _WIN32 // On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does)
SDL_SetHint(SDL_HINT_WINDOWS_INTRESOURCE_ICON, "0");
SDL_SetHint(SDL_HINT_WINDOWS_INTRESOURCE_ICON_SMALL, "1");
#endif
SDL_InitSubSystem(SDL_INIT_VIDEO);
SDL_Window *window;
switch (conf.display_mode)
{
case 1:
@ -218,28 +216,23 @@ int main(int argc, char *argv[])
windowHeight = WINDOW_HEIGHT * 2;
}
window = CreateWindow(lpWindowName, windowWidth, windowHeight);
if (window == NULL)
return 0;
#ifdef FIX_BUGS
if (conf.display_mode == 1)
{
if (!StartDirectDraw(window, 0))
if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0))
return 0;
}
else
{
if (!StartDirectDraw(window, 1))
if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1))
return 0;
}
#else
// Doesn't handle StartDirectDraw failing
if (conf.display_mode == 1)
StartDirectDraw(window, 0);
StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0);
else
StartDirectDraw(window, 1);
StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1);
#endif
break;
@ -251,17 +244,12 @@ int main(int argc, char *argv[])
windowWidth = WINDOW_WIDTH * 2;
windowHeight = WINDOW_HEIGHT * 2;
window = CreateWindow(lpWindowName, windowWidth, windowHeight);
if (window == NULL)
return 0;
#ifdef FIX_BUGS
if (!StartDirectDraw(window, 2))
if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2))
return 0;
#else
// Doesn't handle StartDirectDraw failing
StartDirectDraw(window, 2);
StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2);
#endif
bFullscreen = TRUE;
@ -274,21 +262,10 @@ int main(int argc, char *argv[])
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
#endif
// Set up the window icon and cursor
const unsigned char *resource_data;
// Set up the cursor
size_t resource_size;
SDL_RWops *rwops;
#ifndef _WIN32 // On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does)
resource_data = FindResource("ICON_MINI", "ICON", &resource_size);
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
resource_data = FindResource("CURSOR_NORMAL", "CURSOR", &resource_size);
rwops = SDL_RWFromConstMem(resource_data, resource_size);
const unsigned char *resource_data = FindResource("CURSOR_NORMAL", "CURSOR", &resource_size);
SDL_RWops *rwops = SDL_RWFromConstMem(resource_data, resource_size);
SDL_Surface *cursor_surface = SDL_LoadBMP_RW(rwops, 1);
SDL_SetColorKey(cursor_surface, SDL_TRUE, SDL_MapRGB(cursor_surface->format, 0xFF, 0, 0xFF));
SDL_Cursor *cursor = SDL_CreateColorCursor(cursor_surface, 0, 0);
@ -315,7 +292,6 @@ int main(int argc, char *argv[])
{
SDL_FreeCursor(cursor);
SDL_FreeSurface(cursor_surface);
SDL_DestroyWindow(window);
return 1;
}
@ -343,7 +319,6 @@ int main(int argc, char *argv[])
SDL_FreeCursor(cursor);
SDL_FreeSurface(cursor_surface);
SDL_DestroyWindow(window);
return 1;
}