aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/je_window.cpp
blob: 8c36c85cce2311bf1c8384e8d74e79c6430e20b3 (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
#include "../core/je_configuration.h"
#if defined(jin_graphics)

#include <iostream>

#include "../utils/je_utils.h"
#include "../audio/sdl/je_sdl_audio.h"
#include "../utils/je_log.h"

#include "shader/je_shader.h"
#include "je_window.h"
#include "je_gl.h"
#include "je_canvas.h"

namespace JinEngine
{
	namespace Graphics
	{

		bool Window::initSystem(const SettingBase* s)
		{
	#if defined(jin_debug)
			Loghelper::log(Loglevel::LV_INFO, "Init window system");
	#endif

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

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

			if (wnd)
			{
				SDL_DestroyWindow(wnd);
				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);

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

			wnd = SDL_CreateWindow(title, wx, wy, size.w, size.h, flag);
			if (wnd == NULL) 
				return false;
			ctx = SDL_GL_CreateContext(wnd);
			if (ctx == NULL) 
				return false;
			SDL_GL_SetSwapInterval(vsync ? 1 : 0);
			SDL_GL_MakeCurrent(wnd, ctx);
			// default configuration
			gl.setClearColor(0, 0, 0, 0xff);
			gl.pushColor(0xff, 0xff, 0xff, 0xff);
			gl.enable(GL_BLEND);
			gl.enable(GL_TEXTURE_2D);
			gl.setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			// avoid white screen blink on windows 
			swapBuffers();
			// bind to default canvas 
			Canvas::unbind();
			Shader::unuse();
			return true; 
		}

		void Window::quitSystem()
		{
			// disable opengl
			gl.disable(GL_BLEND);
			gl.disable(GL_TEXTURE_2D);
			// close window 
			SDL_DestroyWindow(wnd);
			SDL_Quit();
		}

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

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

	} // namespace Graphics
} // namespace JinEngine

#endif // defined(jin_graphics)