Skip to content

Commit 8e7ed3e

Browse files
committed
Added ability to set initial window size when starting a game.
1 parent 7e2497e commit 8e7ed3e

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed

docs/general/project_properties.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Project properties define how the game will be configured before running. An ex
1212
"width": 800,
1313
"height": 600
1414
},
15+
"window_size": {
16+
"width": 800,
17+
"height": 600
18+
},
1519
"colliders_visible": true,
1620
"pixel_snap": true,
1721
"target_fps": 60,
@@ -77,7 +81,11 @@ First scene loaded for the game.
7781

7882
`base_resolution`
7983

80-
Base resolution of the game
84+
Base resolution of the game.
85+
86+
`window_size`
87+
88+
The initial window size when starting the game.
8189

8290
`colliders_visible`
8391

src/core/ecs/system/systems/collision_entity_system.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class CollisionEntitySystem : public EntitySystem {
5252
Vector2 collisionScreenScale = Vector2(transform2DComponent.scale.x * parentTransform.scale.x * colliderComponent.collider.w * camera.zoom.x,
5353
transform2DComponent.scale.y * parentTransform.scale.y * colliderComponent.collider.h * camera.zoom.y);
5454
ProjectProperties *projectProperties = ProjectProperties::GetInstance();
55-
collisionScreenScale.x *= windowWidth / static_cast<float>(projectProperties->windowWidth);
56-
collisionScreenScale.y *= windowHeight / static_cast<float>(projectProperties->windowHeight);
55+
collisionScreenScale.x *= windowWidth / static_cast<float>(projectProperties->resolutionWidth);
56+
collisionScreenScale.y *= windowHeight / static_cast<float>(projectProperties->resolutionHeight);
5757
return Rect2(collisionScreenPosition, collisionScreenScale);
5858
}
5959

src/core/game.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ void Game::InitializeRendering() {
203203
projectProperties->gameTitle.c_str(),
204204
SDL_WINDOWPOS_CENTERED,
205205
SDL_WINDOWPOS_CENTERED,
206-
projectProperties->windowWidth,
207-
projectProperties->windowHeight,
206+
projectProperties->intitialWindowWidth,
207+
projectProperties->intitialWindowHeight,
208208
renderContext->windowFlags);
209209
renderContext->gl_context = SDL_GL_CreateContext(renderContext->window);
210-
renderContext->currentWindowWidth = projectProperties->windowWidth;
211-
renderContext->currentWindowHeight = projectProperties->windowHeight;
210+
renderContext->currentWindowWidth = projectProperties->intitialWindowWidth;
211+
renderContext->currentWindowHeight = projectProperties->intitialWindowHeight;
212212

213213
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) {
214214
logger->Error("Couldn't initialize glad");

src/core/project_properties.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ class ProjectProperties {
8080
gameTitle = JsonHelper::Get<std::string>(projectConfigurationsJson, "game_title");
8181
initialScenePath = JsonHelper::Get<std::string>(projectConfigurationsJson, "initial_scene");
8282
nlohmann::json baseResolutionJson = JsonHelper::Get<nlohmann::json>(projectConfigurationsJson, "base_resolution");
83-
windowWidth = JsonHelper::Get<int>(baseResolutionJson, "width");
84-
windowHeight = JsonHelper::Get<int>(baseResolutionJson, "height");
83+
resolutionWidth = JsonHelper::Get<int>(baseResolutionJson, "width");
84+
resolutionHeight = JsonHelper::Get<int>(baseResolutionJson, "height");
85+
nlohmann::json windowSizeJson = JsonHelper::Get<nlohmann::json>(projectConfigurationsJson, "window_size");
86+
intitialWindowWidth = JsonHelper::Get<int>(windowSizeJson, "width");
87+
intitialWindowHeight = JsonHelper::Get<int>(windowSizeJson, "height");
8588
areColliderVisible = JsonHelper::Get<bool>(projectConfigurationsJson, "colliders_visible");
8689
pixelSnap = JsonHelper::GetDefault<bool>(projectConfigurationsJson, "pixel_snap", false);
8790
targetFPS = JsonHelper::Get<unsigned int>(projectConfigurationsJson, "target_fps");
@@ -167,8 +170,10 @@ class ProjectProperties {
167170
}
168171
public:
169172
std::string gameTitle = "Seika Engine";
170-
int windowWidth = 800;
171-
int windowHeight = 600;
173+
int resolutionWidth = 800;
174+
int resolutionHeight = 600;
175+
int intitialWindowWidth = resolutionWidth;
176+
int intitialWindowHeight = resolutionHeight;
172177
Color backgroundDrawColor = Color::NormalizedColor(20, 20, 20);
173178
bool areColliderVisible = false;
174179
std::string initialScenePath;

src/core/rendering/font_renderer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ void FontRenderer::Draw(Font *font, const std::string &text, float x, float y, f
1414
glBindVertexArray(font->VAO);
1515

1616
y = ConvertMinMax(y,
17-
static_cast<float>(projectProperties->windowHeight),
17+
static_cast<float>(projectProperties->resolutionHeight),
1818
0,
1919
0,
20-
static_cast<float>(projectProperties->windowHeight));
20+
static_cast<float>(projectProperties->resolutionHeight));
2121
y -= font->GetSize() * 0.8f;
2222

2323
// iterate through all characters
@@ -57,7 +57,7 @@ void FontRenderer::Draw(Font *font, const std::string &text, float x, float y, f
5757
}
5858

5959
void FontRenderer::UpdateProjection() {
60-
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(projectProperties->windowWidth), 0.0f, static_cast<float>(projectProperties->windowHeight), -1.0f, 1.0f);
60+
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(projectProperties->resolutionWidth), 0.0f, static_cast<float>(projectProperties->resolutionHeight), -1.0f, 1.0f);
6161
shader.Use();
6262
shader.SetMatrix4Float("projection", projection);
6363
}

src/core/rendering/renderer3d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void Renderer3D::Initialize() {
6060
void Renderer3D::Render(CameraManager *cameraManager) {
6161
glEnable(GL_DEPTH_TEST);
6262
static ProjectProperties *projectProperties = ProjectProperties::GetInstance();
63-
const float aspectRatio = projectProperties->windowWidth / projectProperties->windowHeight;
63+
const float aspectRatio = projectProperties->resolutionWidth / projectProperties->resolutionHeight;
6464
Camera3D camera = cameraManager->GetCurrentCamera3D();
6565
glm::mat4 projection = glm::perspective(glm::radians(camera.fieldOfView), aspectRatio, 0.1f, 100.0f);
6666
glm::mat4 view = CameraHandler::GetCameraViewMatrix(camera);

src/core/rendering/sprite_renderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void SpriteRenderer::Draw(Texture *texture2D, Rect2 sourceRectangle, Rect2 desti
111111

112112
void SpriteRenderer::UpdateProjection() {
113113
shader.Use();
114-
projection = glm::ortho(0.0f, static_cast<float>(projectProperties->windowWidth), static_cast<float>(projectProperties->windowHeight), 0.0f, -1.0f, 1.0f);
114+
projection = glm::ortho(0.0f, static_cast<float>(projectProperties->resolutionWidth), static_cast<float>(projectProperties->resolutionHeight), 0.0f, -1.0f, 1.0f);
115115
shader.SetMatrix4Float("projection", projection);
116116
}
117117

0 commit comments

Comments
 (0)