Skip to content

Commit 8ab074b

Browse files
authored
Merge pull request #46 from morganchristiansson/resize-window-small
Allow window resizing
2 parents 4a2d0df + e40eab9 commit 8ab074b

6 files changed

Lines changed: 52 additions & 6 deletions

File tree

CGame.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CGame
4242
CFont lastFps;
4343

4444
Uint32 lastFrameTime = 0;
45+
unsigned suppressResizeEvents_ = 0;
4546

4647
// structure for mouse cursor
4748
struct
@@ -65,6 +66,7 @@ class CGame
6566
std::unique_ptr<CMap> MapObj;
6667

6768
void SetAppIcon();
69+
void RecreateDisplayResources();
6870

6971
public:
7072
CGame(Extent GameResolution_, bool fullscreen_);
@@ -74,6 +76,7 @@ class CGame
7476

7577
bool Init();
7678
bool ReCreateWindow();
79+
void UpdateDisplaySize(const Extent& newSize);
7780

7881
void EventHandling(SDL_Event* Event);
7982

CGame_Event.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ void CGame::EventHandling(SDL_Event* Event)
6363

6464
switch(Event->key.keysym.sym)
6565
{
66-
case SDLK_F2: fullscreen = !fullscreen; break;
66+
case SDLK_RETURN:
67+
case SDLK_KP_ENTER:
68+
if(Event->key.keysym.mod & KMOD_ALT)
69+
fullscreen = !fullscreen;
70+
break;
6771

6872
#ifdef _ADMINMODE
6973
case SDLK_F3: // if CTRL and ALT are pressed
@@ -377,6 +381,20 @@ void CGame::EventHandling(SDL_Event* Event)
377381
break;
378382
}
379383

384+
case SDL_WINDOWEVENT:
385+
{
386+
if(Event->window.event == SDL_WINDOWEVENT_RESIZED)
387+
{
388+
if(suppressResizeEvents_ > 0)
389+
{
390+
suppressResizeEvents_--;
391+
break; // Skip stale event from our own window recreation
392+
}
393+
UpdateDisplaySize(Extent(Event->window.data1, Event->window.data2));
394+
}
395+
break;
396+
}
397+
380398
case SDL_QUIT: Running = false; break;
381399

382400
default: break;

CGame_Init.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "CGame.h"
77
#include "CIO/CFile.h"
8+
#include "CIO/CMenu.h"
9+
#include "CIO/CWindow.h"
810
#include "CMap.h"
911
#include "CSurface.h"
1012
#include "SGE/sge_blib.h"
@@ -17,27 +19,44 @@
1719

1820
bool CGame::ReCreateWindow()
1921
{
22+
suppressResizeEvents_ = 3;
2023
displayTexture_.reset();
2124
renderer_.reset();
2225
window_.reset();
2326
window_.reset(SDL_CreateWindow("Return to the Roots Map editor [BETA]", SDL_WINDOWPOS_CENTERED,
2427
SDL_WINDOWPOS_CENTERED, GameResolution.x, GameResolution.y,
25-
fullscreen ? SDL_WINDOW_FULLSCREEN : 0));
28+
fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE));
2629
if(!window_)
2730
return false;
2831
renderer_.reset(SDL_CreateRenderer(window_.get(), -1, 0));
2932
if(!renderer_)
3033
return false;
31-
displayTexture_ = makeSdlTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, GameResolution.x,
32-
GameResolution.y);
33-
Surf_Display = makeRGBSurface(GameResolution.x, GameResolution.y, true);
34+
RecreateDisplayResources();
3435
if(!displayTexture_ || !Surf_Display)
3536
return false;
3637

3738
SetAppIcon();
3839
return true;
3940
}
4041

42+
void CGame::RecreateDisplayResources()
43+
{
44+
displayTexture_.reset();
45+
displayTexture_ = makeSdlTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, GameResolution.x,
46+
GameResolution.y);
47+
Surf_Display = makeRGBSurface(GameResolution.x, GameResolution.y, true);
48+
}
49+
50+
void CGame::UpdateDisplaySize(const Extent& newSize)
51+
{
52+
GameResolution = newSize;
53+
RecreateDisplayResources();
54+
for(auto& menu : Menus)
55+
menu->resetSurface();
56+
for(auto& wnd : Windows)
57+
wnd->resetSurface();
58+
}
59+
4160
bool CGame::Init()
4261
{
4362
std::cout << "Return to the Roots Map editor\n";

CGame_Render.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void CGame::SetAppIcon()
3939

4040
void CGame::Render()
4141
{
42+
suppressResizeEvents_ = 0;
4243
if(Extent(Surf_Display->w, Surf_Display->h) != GameResolution
4344
|| fullscreen != ((SDL_GetWindowFlags(window_.get()) & SDL_WINDOW_FULLSCREEN) != 0))
4445
{

CIO/CControlContainer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class CControlContainer
6868
}
6969
void setWaste() { waste = true; }
7070
bool isWaste() const { return waste; }
71+
void resetSurface()
72+
{
73+
surface.reset();
74+
needRender = true;
75+
}
7176
// Methods
7277
CButton* addButton(void callback(int), int clickedParam, Uint16 x = 0, Uint16 y = 0, Uint16 w = 20, Uint16 h = 20,
7378
int color = BUTTON_GREY, const char* text = nullptr, int picture = -1);

callbacks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ void callback::EditorHelpMenu(int Param)
611611
"..............................F1");
612612
SelectBoxHelp->addOption(
613613
"Window/"
614-
"Fullscreen........................................................................................F2");
614+
"Fullscreen...................................................................................Alt+Enter");
615615
SelectBoxHelp->addOption(
616616
"Zoom in/normal/out......................................................................F5/F6/"
617617
"F7");

0 commit comments

Comments
 (0)