Skip to content

Commit

Permalink
Added ability to set initial window size when starting a game.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chukobyte committed Jan 8, 2022
1 parent 7e2497e commit 8e7ed3e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
10 changes: 9 additions & 1 deletion docs/general/project_properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Project properties define how the game will be configured before running. An ex
"width": 800,
"height": 600
},
"window_size": {
"width": 800,
"height": 600
},
"colliders_visible": true,
"pixel_snap": true,
"target_fps": 60,
Expand Down Expand Up @@ -77,7 +81,11 @@ First scene loaded for the game.

`base_resolution`

Base resolution of the game
Base resolution of the game.

`window_size`

The initial window size when starting the game.

`colliders_visible`

Expand Down
4 changes: 2 additions & 2 deletions src/core/ecs/system/systems/collision_entity_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class CollisionEntitySystem : public EntitySystem {
Vector2 collisionScreenScale = Vector2(transform2DComponent.scale.x * parentTransform.scale.x * colliderComponent.collider.w * camera.zoom.x,
transform2DComponent.scale.y * parentTransform.scale.y * colliderComponent.collider.h * camera.zoom.y);
ProjectProperties *projectProperties = ProjectProperties::GetInstance();
collisionScreenScale.x *= windowWidth / static_cast<float>(projectProperties->windowWidth);
collisionScreenScale.y *= windowHeight / static_cast<float>(projectProperties->windowHeight);
collisionScreenScale.x *= windowWidth / static_cast<float>(projectProperties->resolutionWidth);
collisionScreenScale.y *= windowHeight / static_cast<float>(projectProperties->resolutionHeight);
return Rect2(collisionScreenPosition, collisionScreenScale);
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ void Game::InitializeRendering() {
projectProperties->gameTitle.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
projectProperties->windowWidth,
projectProperties->windowHeight,
projectProperties->intitialWindowWidth,
projectProperties->intitialWindowHeight,
renderContext->windowFlags);
renderContext->gl_context = SDL_GL_CreateContext(renderContext->window);
renderContext->currentWindowWidth = projectProperties->windowWidth;
renderContext->currentWindowHeight = projectProperties->windowHeight;
renderContext->currentWindowWidth = projectProperties->intitialWindowWidth;
renderContext->currentWindowHeight = projectProperties->intitialWindowHeight;

if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) {
logger->Error("Couldn't initialize glad");
Expand Down
13 changes: 9 additions & 4 deletions src/core/project_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ class ProjectProperties {
gameTitle = JsonHelper::Get<std::string>(projectConfigurationsJson, "game_title");
initialScenePath = JsonHelper::Get<std::string>(projectConfigurationsJson, "initial_scene");
nlohmann::json baseResolutionJson = JsonHelper::Get<nlohmann::json>(projectConfigurationsJson, "base_resolution");
windowWidth = JsonHelper::Get<int>(baseResolutionJson, "width");
windowHeight = JsonHelper::Get<int>(baseResolutionJson, "height");
resolutionWidth = JsonHelper::Get<int>(baseResolutionJson, "width");
resolutionHeight = JsonHelper::Get<int>(baseResolutionJson, "height");
nlohmann::json windowSizeJson = JsonHelper::Get<nlohmann::json>(projectConfigurationsJson, "window_size");
intitialWindowWidth = JsonHelper::Get<int>(windowSizeJson, "width");
intitialWindowHeight = JsonHelper::Get<int>(windowSizeJson, "height");
areColliderVisible = JsonHelper::Get<bool>(projectConfigurationsJson, "colliders_visible");
pixelSnap = JsonHelper::GetDefault<bool>(projectConfigurationsJson, "pixel_snap", false);
targetFPS = JsonHelper::Get<unsigned int>(projectConfigurationsJson, "target_fps");
Expand Down Expand Up @@ -167,8 +170,10 @@ class ProjectProperties {
}
public:
std::string gameTitle = "Seika Engine";
int windowWidth = 800;
int windowHeight = 600;
int resolutionWidth = 800;
int resolutionHeight = 600;
int intitialWindowWidth = resolutionWidth;
int intitialWindowHeight = resolutionHeight;
Color backgroundDrawColor = Color::NormalizedColor(20, 20, 20);
bool areColliderVisible = false;
std::string initialScenePath;
Expand Down
6 changes: 3 additions & 3 deletions src/core/rendering/font_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ void FontRenderer::Draw(Font *font, const std::string &text, float x, float y, f
glBindVertexArray(font->VAO);

y = ConvertMinMax(y,
static_cast<float>(projectProperties->windowHeight),
static_cast<float>(projectProperties->resolutionHeight),
0,
0,
static_cast<float>(projectProperties->windowHeight));
static_cast<float>(projectProperties->resolutionHeight));
y -= font->GetSize() * 0.8f;

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

void FontRenderer::UpdateProjection() {
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(projectProperties->windowWidth), 0.0f, static_cast<float>(projectProperties->windowHeight), -1.0f, 1.0f);
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(projectProperties->resolutionWidth), 0.0f, static_cast<float>(projectProperties->resolutionHeight), -1.0f, 1.0f);
shader.Use();
shader.SetMatrix4Float("projection", projection);
}
2 changes: 1 addition & 1 deletion src/core/rendering/renderer3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void Renderer3D::Initialize() {
void Renderer3D::Render(CameraManager *cameraManager) {
glEnable(GL_DEPTH_TEST);
static ProjectProperties *projectProperties = ProjectProperties::GetInstance();
const float aspectRatio = projectProperties->windowWidth / projectProperties->windowHeight;
const float aspectRatio = projectProperties->resolutionWidth / projectProperties->resolutionHeight;
Camera3D camera = cameraManager->GetCurrentCamera3D();
glm::mat4 projection = glm::perspective(glm::radians(camera.fieldOfView), aspectRatio, 0.1f, 100.0f);
glm::mat4 view = CameraHandler::GetCameraViewMatrix(camera);
Expand Down
2 changes: 1 addition & 1 deletion src/core/rendering/sprite_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void SpriteRenderer::Draw(Texture *texture2D, Rect2 sourceRectangle, Rect2 desti

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

Expand Down

0 comments on commit 8e7ed3e

Please sign in to comment.