aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/graphics/window.cpp
blob: 479961b61c5306d5b7f6122457b4c86defde9328 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "../core/configuration.h"
#if defined(jin_graphics)

#include <iostream>

#include "../common/exception.h"
#include "../utils/utils.h"
#include "../audio/sdl/sdl_audio.h"
#include "../utils/log.h"

#include "shaders/shader.h"
#include "window.h"
#include "opengl.h"
#include "canvas.h"

using namespace JinEngine::Graphics::Shaders;

namespace JinEngine
{
    namespace Graphics
    {
    
        bool Window::startSystem(const SettingBase* s)
        {
            jin_log_info("Initialize window system.");

            if (SDL_Init(SDL_INIT_VIDEO) < 0)
                return false; 

            const Setting* setting = (Setting*)s;
            mSize.w()          = setting->width;
            mSize.h()          = setting->height;
            mFps               = setting->fps;
            bool vsync         = setting->vsync;
            const char* title  = setting->title;
            const char* icon   = setting->icon;

            if (mWnd)
            {
                SDL_DestroyWindow(mWnd);
                SDL_FlushEvent(SDL_WINDOWEVENT);
            }

            SDL_GLContext ctx = NULL;

            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);
            SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
            SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
            SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0);

            int wx = SDL_WINDOWPOS_UNDEFINED,
                wy = SDL_WINDOWPOS_UNDEFINED;
        
            int flag = SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL;
            if (setting->fullscreen) flag |= SDL_WINDOW_FULLSCREEN;
            if (setting->resizable) flag |= SDL_WINDOW_RESIZABLE;

            mWnd = SDL_CreateWindow(title, wx, wy, mSize.w(), mSize.h(), flag);
            if (mWnd == NULL)
                return false;

            // Set window icon.
            //try
            //{
            //    Bitmap* bitmap = new Bitmap(icon);
            //    SDL_Surface *surface;
            //    Color* pixels = const_cast<Color*>(bitmap->getPixels());
            //    uint w = bitmap->getWidth(), h = bitmap->getHeight();
            //    surface = SDL_CreateRGBSurfaceFrom(
            //        pixels, w, h, 32, w * 4, Color::RMASK, Color::GMASK, Color::BMASK, Color::AMASK);
            //    SDL_SetWindowIcon(mWnd, surface);
            //    SDL_FreeSurface(surface);
            //} catch (...) {}

            ctx = SDL_GL_CreateContext(mWnd);
            if (ctx == NULL)
                return false;

            gl.loadGL();

            SDL_GL_SetSwapInterval(vsync ? 1 : 0);
            SDL_GL_MakeCurrent(mWnd, ctx);

            // Default configuration.
            gl.init();

            return true;
        }

        void Window::quitSystem()
        {
            jin_log_info("Quit window system.");

            SDL_DestroyWindow(mWnd);
        }

        void Window::present()
        {
            if (mWnd)
                SDL_GL_SwapWindow(mWnd);
            gl.resetStats();
        }

        void Window::setTitle(const char* title)
        {
            SDL_SetWindowTitle(mWnd, title);
        };

    } // namespace Graphics
} // namespace JinEngine

#endif // defined(jin_graphics)