88#include " CIO/CMenu.h"
99#include " CIO/CWindow.h"
1010#include " CMap.h"
11- #include " CSurface.h"
1211#include " callbacks.h"
1312#include " globals.h"
1413#include " lua/GameDataLoader.h"
14+ #include < glad/glad.h>
1515#include < iostream>
16- #include < vector>
1716
18- bool CGame::ReCreateWindow ()
17+ bool CGame::CreateWindow ()
1918{
20- suppressResizeEvents_ = 3 ;
21- displayTexture_.reset ();
22- renderer_.reset ();
23- window_.reset ();
19+ if (window_)
20+ return false ;
21+
2422 window_.reset (SDL_CreateWindow (" Return to the Roots Map editor [BETA]" , SDL_WINDOWPOS_CENTERED ,
2523 SDL_WINDOWPOS_CENTERED , GameResolution.x , GameResolution.y ,
26- fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE ));
24+ SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE ));
2725 if (!window_)
2826 return false ;
29- renderer_.reset (SDL_CreateRenderer (window_.get (), -1 , 0 ));
30- if (!renderer_)
27+
28+ glContext_ = SDL_GL_CreateContext (window_.get ());
29+ if (!glContext_ || !gladLoadGLLoader ((GLADloadproc)SDL_GL_GetProcAddress))
3130 return false ;
32- RecreateDisplayResources ();
33- if (!displayTexture_ || !Surf_Display)
31+
32+ glEnable (GL_TEXTURE_2D );
33+ glEnable (GL_BLEND );
34+ glBlendFunc (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
35+ glDisable (GL_DEPTH_TEST );
36+ glClearColor (0 , 0 , 0 , 1 );
37+
38+ SDL_ShowWindow (window_.get ());
39+
40+ ApplyWindowChanges ();
41+ if (!displayTexture_.isValid () || !Surf_Display)
3442 return false ;
3543
3644 SetAppIcon ();
45+
3746 return true ;
3847}
3948
40- void CGame::RecreateDisplayResources ()
49+ void CGame::ApplyWindowChanges ()
4150{
42- displayTexture_.reset ();
43- displayTexture_ = makeSdlTexture (renderer_, SDL_PIXELFORMAT_ARGB8888 , SDL_TEXTUREACCESS_STREAMING , GameResolution.x ,
44- GameResolution.y );
45- Surf_Display = makeRGBSurface (GameResolution.x , GameResolution.y , true );
51+ if (!window_)
52+ return ;
53+
54+ if (fullscreen)
55+ {
56+ SDL_DisplayMode dm;
57+ SDL_zero (dm);
58+ dm.w = static_cast <int >(GameResolution.x );
59+ dm.h = static_cast <int >(GameResolution.y );
60+ dm.format = 0 ; // let SDL pick a supported format
61+ dm.refresh_rate = 0 ;
62+ if (SDL_SetWindowDisplayMode (window_.get (), &dm) != 0 )
63+ {
64+ std::cerr << " SDL_SetWindowDisplayMode failed: " << SDL_GetError () << std::endl;
65+ return ;
66+ }
67+
68+ const Uint32 flags = SDL_GetWindowFlags (window_.get ());
69+ if (!(flags & SDL_WINDOW_FULLSCREEN ))
70+ {
71+ if (SDL_SetWindowFullscreen (window_.get (), SDL_WINDOW_FULLSCREEN ) != 0 )
72+ {
73+ std::cerr << " SDL_SetWindowFullscreen failed: " << SDL_GetError () << std::endl;
74+ return ;
75+ }
76+ } else if (GameResolution != appliedResolution_)
77+ {
78+ // Already fullscreen and the resolution changed. Toggle fullscreen off and
79+ // back on so SDL/Wayland actually applies the new display mode.
80+ if (SDL_SetWindowFullscreen (window_.get (), 0 ) != 0 )
81+ {
82+ std::cerr << " SDL_SetWindowFullscreen(0) failed: " << SDL_GetError () << std::endl;
83+ return ;
84+ }
85+ SDL_SetWindowSize (window_.get (), GameResolution.x , GameResolution.y );
86+ if (SDL_SetWindowFullscreen (window_.get (), SDL_WINDOW_FULLSCREEN ) != 0 )
87+ {
88+ std::cerr << " SDL_SetWindowFullscreen failed: " << SDL_GetError () << std::endl;
89+ return ;
90+ }
91+ }
92+ } else
93+ {
94+ if (SDL_SetWindowFullscreen (window_.get (), 0 ) != 0 )
95+ {
96+ std::cerr << " SDL_SetWindowFullscreen failed: " << SDL_GetError () << std::endl;
97+ return ;
98+ }
99+ SDL_SetWindowSize (window_.get (), GameResolution.x , GameResolution.y );
100+ SDL_SetWindowPosition (window_.get (), SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED );
101+ }
102+
103+ UpdateDisplaySize (GameResolution);
104+ }
105+
106+ void CGame::setGLViewport ()
107+ {
108+ if (!window_)
109+ return ;
110+ int w = 0 , h = 0 ;
111+ SDL_GL_GetDrawableSize (window_.get (), &w, &h);
112+ if (w == 0 || h == 0 )
113+ return ;
114+ glViewport (0 , 0 , w, h);
115+ glMatrixMode (GL_PROJECTION );
116+ glLoadIdentity ();
117+ glOrtho (0 , GameResolution.x , GameResolution.y , 0 , -1 , 1 );
118+ glMatrixMode (GL_MODELVIEW );
119+ glLoadIdentity ();
46120}
47121
48122void CGame::UpdateDisplaySize (const Extent& newSize)
49123{
50124 GameResolution = newSize;
51- RecreateDisplayResources ();
125+ appliedResolution_ = GameResolution;
126+ appliedFullscreen_ = fullscreen;
127+
128+ Surf_Display = makeRGBSurface (GameResolution.x , GameResolution.y , true );
129+ displayTexture_.createEmpty (GameResolution);
130+
131+ setGLViewport ();
52132 for (auto & menu : Menus)
133+ {
53134 menu->resetSurface ();
135+ }
54136 for (auto & wnd : Windows)
55137 wnd->resetSurface ();
56138}
@@ -62,7 +144,7 @@ bool CGame::Init()
62144 SDL_ShowCursor (SDL_DISABLE );
63145
64146 std::cout << " Create Window..." ;
65- if (!ReCreateWindow ())
147+ if (!CreateWindow ())
66148 {
67149 std::cout << " failure" ;
68150 return false ;
@@ -94,10 +176,14 @@ bool CGame::Init()
94176 }
95177 }
96178
179+ // Create texture for splash background
180+ splashBg_.load (global::bmpArray[SPLASHSCREEN_LOADING_S2SCREEN ].surface .get (), true );
181+
97182 // std::cout << "\nShow loading screen...";
98183 showLoadScreen = true ;
99- CSurface::DrawStretched (Surf_Display, global::bmpArray[SPLASHSCREEN_LOADING_S2SCREEN ].surface );
100- RenderPresent ();
184+ glClear (GL_COLOR_BUFFER_BIT );
185+ splashBg_.Draw (Rect (0 , 0 , GameResolution.x , GameResolution.y ));
186+ SDL_GL_SwapWindow (window_.get ());
101187
102188 GameDataLoader gdLoader (global::worldDesc);
103189 if (!gdLoader.Load ())
@@ -252,5 +338,10 @@ bool CGame::Init()
252338 // create the mainmenu
253339 callback::mainmenu (INITIALIZING_CALL );
254340
341+ // Create textures for cursor
342+ cursor_.load (global::bmpArray[CURSOR ].surface .get ());
343+ cursorClicked_.load (global::bmpArray[CURSOR_CLICKED ].surface .get ());
344+ cross_.load (global::bmpArray[CROSS ].surface .get ());
345+
255346 return true ;
256347}
0 commit comments