aboutsummaryrefslogtreecommitdiff
path: root/src/render/window.cpp
blob: a12c92fe7dd251866df2ae27b40dfa4ec62a6582 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "window.h"
#include "libs/GLee/GLee.h"
#include "canvas.h"
namespace jin
{
namespace render
{

    Window* Window::g_wnd = 0; 
    
    Window* Window::get()
    {
        return (g_wnd ? g_wnd : (g_wnd = new Window()));
    }
    
    Window::Window(): wnd(0), ctx(0)
    {
    }
    
    Window::~Window()
    {
    }
    
    void Window::init(int pw, int ph, const char* t)
    {
        w = pw;
        h = ph;

        if (wnd)
        {
            SDL_DestroyWindow(wnd); 
            SDL_FlushEvent(SDL_WINDOWEVENT);
        }
        
        if (ctx)
        {
            SDL_GL_DeleteContext(ctx);
        }
        
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

        Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL;

        int wx = SDL_WINDOWPOS_UNDEFINED,
            wy = SDL_WINDOWPOS_UNDEFINED;

        /* Create window */
        wnd = SDL_CreateWindow(t, wx, wy, w, h, flags);

        // Create an opengl context 
        ctx = SDL_GL_CreateContext(wnd);
        SDL_GL_MakeCurrent(wnd, ctx);

        // Default clear color
        glClearColor(0.f, 0.f, 0.f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

        // Default render color
        glColor4f(1, 1, 1, 1);

        /**
        * Set the viewport to top-left corner. 
        * Bind to the default render buffer. 
        */ 
        Canvas::unbind();
        
        // Swap window buffer
        swapBuffers();
    }
    
    SDL_Window* Window::getWnd()
    {
        return wnd; 
    }

    SDL_GLContext Window::getCtx()
    {
        return ctx; 
    }

    int Window::getW()
    {
        return w; 
    }

    int Window::getH()
    {
        return h;
    }

    void Window::swapBuffers()
    {
        if (wnd) 
            SDL_GL_SwapWindow(wnd);
    }

}
}