aboutsummaryrefslogtreecommitdiff
path: root/src/state.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-05-19 21:41:09 +0200
committerCharles <sircharlesaze@gmail.com>2020-05-19 21:41:09 +0200
commit3b2e7cc2347d88dbd8d7697a7cbd8354e7728fc0 (patch)
tree56cd83dca6d3627844347cc2ca86a6f6255f87d5 /src/state.c
parentb0998910dd974280b3c6f3f65e21bfd5859b117f (diff)
downloadmandelbrot-3b2e7cc2347d88dbd8d7697a7cbd8354e7728fc0.tar.gz
mandelbrot-3b2e7cc2347d88dbd8d7697a7cbd8354e7728fc0.tar.bz2
mandelbrot-3b2e7cc2347d88dbd8d7697a7cbd8354e7728fc0.zip
Added OpenGL boilerplate
Diffstat (limited to 'src/state.c')
-rw-r--r--src/state.c77
1 files changed, 24 insertions, 53 deletions
diff --git a/src/state.c b/src/state.c
index 395d590..b2ea45c 100644
--- a/src/state.c
+++ b/src/state.c
@@ -4,25 +4,32 @@
bool state_init(State *state)
{
SDL_CALL(SDL_Init(SDL_INIT_VIDEO));
+ SDL_CALL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4));
+ SDL_CALL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0));
+ SDL_CALL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE));
+ SDL_CALL(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1));
SDL_CALL(state->window = SDL_CreateWindow(
MANDEL_WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
MANDEL_WINDOW_WIDTH,
MANDEL_WINDOW_HEIGHT,
- SDL_WINDOW_RESIZABLE
+ SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
));
- SDL_CALL(state->renderer = SDL_CreateRenderer(state->window, -1, 0));
- SDL_CALL(state->texture = SDL_CreateTexture(
- state->renderer,
- SDL_PIXELFORMAT_RGB888,
- SDL_TEXTUREACCESS_STREAMING,
- MANDEL_WINDOW_WIDTH,
- MANDEL_WINDOW_HEIGHT
- ));
- state->palette = color_palette_new(NULL, MANDEL_ITERATIONS);
- if (state->palette == NULL)
+ SDL_CALL(state->context = SDL_GL_CreateContext(state->window));
+ assert(glewInit() == GLEW_OK);
+ SDL_CALL(SDL_GL_SetSwapInterval(1));
+ if (!shader_init(&state->shader))
+ {
+ perror(NULL);
return false;
+ }
+ SDL_GL_GetDrawableSize(state->window, &state->width, &state->height);
+ GL_CALL(glViewport(0, 0, state->width, state->height));
+
+ /* state->palette = color_palette_new(NULL, MANDEL_ITERATIONS); */
+ /* if (state->palette == NULL) */
+ /* return false; */
state->iterations = MANDEL_ITERATIONS;
state->real_start = -2.0;
state->real_end = 2.0;
@@ -37,55 +44,19 @@ void state_run(State *state)
while (state->running)
{
event_handle(state);
+ glClearColor(0.2, 0.3, 0.2, 1.0);
+ glClear(GL_COLOR_BUFFER_BIT);
- double real_step;
- double imag_step;
- double real;
- double imag;
- void *pixels;
- int pitch;
- int width;
- int height;
-
- SDL_CALL(SDL_LockTexture(state->texture, NULL, &pixels, &pitch));
- SDL_CALL(SDL_QueryTexture(state->texture, NULL, NULL, &width, &height));
-
- real_step = (state->real_end - state->real_start) / width;
- imag_step = (state->imag_end - state->imag_start) / height;
- imag = state->imag_start;
-
- uint32_t render_start_time = SDL_GetTicks();
-
- for (int i = 0; i < height; i++)
- {
- real = state->real_start;
- for (int j = 0; j < width; j++)
- {
- int n = mandelbrot(real, imag, state->iterations);
- Color c = state->palette[n];
- ((Color*)pixels)[i * width + j] = c;
- real += real_step;
- }
- imag += imag_step;
- }
-
- uint32_t render_end_time = SDL_GetTicks();
-
- printf("%ums\r", (render_end_time - render_start_time));
- fflush(stdout);
-
- SDL_UnlockTexture(state->texture);
- SDL_CALL(SDL_RenderCopy(state->renderer, state->texture, NULL, NULL));
- SDL_RenderPresent(state->renderer);
+ SDL_GL_SwapWindow(state->window);
SDL_Delay(3);
}
}
void state_quit(State *state)
{
- free(state->palette);
- SDL_DestroyTexture(state->texture);
- SDL_DestroyRenderer(state->renderer);
+ /* free(state->palette); */
+ GL_CALL(glDeleteProgram(state->shader.id));
+ SDL_GL_DeleteContext(state->context);
SDL_DestroyWindow(state->window);
SDL_Quit();
}