aboutsummaryrefslogtreecommitdiff
path: root/08-august/src/graphics/window.c
blob: 1a942ca6e8674b66712210bb9e36d1aa2f783b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "window.h"
#include "../util/util.h"
#include <stdlib.h>

window_t* Window_Create(const char* title, Uint32 width, Uint32 height)
{
    window_t* window = (window_t*) malloc(sizeof(window_t));
    window->title = title;
    window->Width = width;
    window->Height = height;
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    window->window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                      width, height, SDL_WINDOW_OPENGL);
    if(window->window == NULL)
        Util_FatalError( "The window could not be created:\n%s", SDL_GetError() );

    window->context = SDL_GL_CreateContext(window->window);

    if(window->context == NULL)
        Util_FatalError( "Context could not be created:\n%s", SDL_GetError() );

    glViewport(0, 0, width, height);
    return window;
}

void Window_Resize(window_t* window, Uint32 width, Uint32 height)
{
    window->Width = width;
    window->Height = height;
    SDL_SetWindowSize(window->window, width, height);
    glViewport(0, 0, width, height);
}

void Window_Update(window_t* window)
{
    SDL_GL_SwapWindow(window->window);
}

void Window_Destroy(window_t* window)
{
    SDL_GL_DeleteContext(window->context);
    SDL_DestroyWindow(window->window);
    free(window);
}