creation of opengl 1.5 context, mixing of gl calls with sdl renderer
This commit is contained in:
@ -50,6 +50,7 @@ typedef struct context {
|
||||
/* it can be changed at runtime; any resulting logic anomalies are bugs */
|
||||
unsigned int update_multiplicity;
|
||||
|
||||
SDL_GLContext *gl_context;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Window *window;
|
||||
uint32_t window_id;
|
||||
|
24
src/main.c
24
src/main.c
@ -11,6 +11,7 @@
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <physfs.h>
|
||||
#include <stb_ds.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
@ -144,18 +145,39 @@ static bool initialize(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
/* init got far enough to create a window */
|
||||
ctx.window = SDL_CreateWindow("emerald",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
RENDER_BASE_WIDTH,
|
||||
RENDER_BASE_HEIGHT,
|
||||
SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE);
|
||||
SDL_WINDOW_ALLOW_HIGHDPI |
|
||||
SDL_WINDOW_RESIZABLE |
|
||||
SDL_WINDOW_OPENGL);
|
||||
if (ctx.window == NULL) {
|
||||
CRY_SDL("Window creation failed.");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ctx.gl_context = SDL_GL_CreateContext(ctx.window);
|
||||
if (!ctx.gl_context) {
|
||||
CRY_SDL("GL context creation failed.");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
SDL_GL_MakeCurrent(ctx.window, ctx.gl_context);
|
||||
|
||||
int glad_status = gladLoadGL();
|
||||
if (glad_status == 0) {
|
||||
CRY("Init", "GLAD failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* might need this to have multiple windows */
|
||||
ctx.window_id = SDL_GetWindowID(ctx.window);
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stb_ds.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <tgmath.h>
|
||||
@ -90,21 +91,6 @@ void push_circle(t_fvec2 position, float radius, t_color color) {
|
||||
}
|
||||
|
||||
|
||||
/* rendering */
|
||||
static void render_background(void) {
|
||||
SDL_SetRenderDrawColor(ctx.renderer, 230, 230, 230, 255);
|
||||
SDL_Rect rect = {
|
||||
0,
|
||||
0,
|
||||
ctx.window_w,
|
||||
ctx.window_h
|
||||
};
|
||||
SDL_RenderFillRect(ctx.renderer, &rect);
|
||||
|
||||
SDL_SetRenderDrawColor(ctx.renderer, 255, 255, 255, 255);
|
||||
}
|
||||
|
||||
|
||||
/* compare functions for the sort in render_sprites */
|
||||
static int cmp_atlases(const void *a, const void *b) {
|
||||
int index_a = ((const struct sprite_primitive *)a)->atlas_index;
|
||||
@ -351,16 +337,36 @@ static void render_circles(void) {
|
||||
}
|
||||
|
||||
|
||||
void render(void) {
|
||||
SDL_SetRenderDrawBlendMode(ctx.renderer, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetRenderDrawColor(ctx.renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(ctx.renderer);
|
||||
SDL_SetRenderDrawColor(ctx.renderer, 255, 255, 255, 255);
|
||||
static void render_space(void) {
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(0, RENDER_BASE_WIDTH, RENDER_BASE_HEIGHT, 0, -1, 1);
|
||||
|
||||
render_background();
|
||||
glBegin(GL_TRIANGLES);
|
||||
glColor4f(0.0, 1.0, 1.0, 1.0);
|
||||
glVertex2f(300.0,210.0);
|
||||
glVertex2f(340.0,215.0);
|
||||
glVertex2f(320.0,250.0);
|
||||
glEnd();
|
||||
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
|
||||
void render(void) {
|
||||
glClearColor((1.0f / 255) * 230,
|
||||
(1.0f / 255) * 230,
|
||||
(1.0f / 255) * 230, 1);
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
render_space();
|
||||
render_sprites();
|
||||
render_rectangles();
|
||||
render_circles();
|
||||
|
||||
SDL_RenderPresent(ctx.renderer);
|
||||
// SDL_RenderPresent(ctx.renderer);
|
||||
SDL_RenderFlush(ctx.renderer);
|
||||
SDL_GL_SwapWindow(ctx.window);
|
||||
}
|
||||
|
Reference in New Issue
Block a user