Compare commits

...

2 Commits

Author SHA1 Message Date
En Yi 44911658d0 Add basic window resizing 2024-10-12 15:02:08 +08:00
En Yi f5fa46aeeb Settle the window size and UIs 2024-10-12 13:35:42 +08:00
6 changed files with 72 additions and 45 deletions

View File

@ -15,6 +15,7 @@ void init_engine(GameEngine_t* engine, Vector2 starting_win_size)
init_assets(&engine->assets);
engine->intended_window_size = starting_win_size;
InitWindow(starting_win_size.x, starting_win_size.y, "raylib");
engine->base_canvas = LoadRenderTexture(starting_win_size.x, starting_win_size.y);
}
void deinit_engine(GameEngine_t* engine)
@ -24,6 +25,7 @@ void deinit_engine(GameEngine_t* engine)
sc_queue_term(&engine->key_buffer);
sc_queue_term(&engine->scene_stack);
sc_heap_term(&engine->scenes_render_order);
UnloadRenderTexture(engine->base_canvas);
CloseAudioDevice();
CloseWindow();
}
@ -239,7 +241,6 @@ static void _internal_render_scene(Scene_t* scene)
Vector2 draw_pos = {draw_rec.x, draw_rec.y};
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
DrawTextureRec(
layer->layer_tex.texture,
draw_rec,
@ -251,8 +252,38 @@ static void _internal_render_scene(Scene_t* scene)
inline void render_scene(Scene_t* scene)
{
BeginDrawing();
BeginTextureMode(scene->engine->base_canvas);
_internal_render_scene(scene);
EndTextureMode();
int curr_width = GetRenderWidth();
int curr_height = GetRenderHeight();
Vector2 original_size = scene->engine->intended_window_size;
float wscale = (curr_width / original_size.x);
float hscale = (curr_height / original_size.y);
float min_dim = (wscale > hscale) ? hscale : wscale;
wscale = min_dim;
hscale = min_dim;
Rectangle draw_rec = {
0,0,
scene->engine->intended_window_size.x,
scene->engine->intended_window_size.y
};
Rectangle draw_pos = {
draw_rec.x * wscale, draw_rec.y * hscale,
draw_rec.width * wscale, draw_rec.height * hscale
};
draw_rec.y *= -1;
BeginDrawing();
ClearBackground((Color){0,0,0,255});
DrawTexturePro(
scene->engine->base_canvas.texture,
draw_rec,
draw_pos,
(Vector2){0,0}, 0.0, WHITE
);
EndDrawing();
}

View File

@ -37,6 +37,7 @@ typedef struct GameEngine {
// This is in case of window scaling, where there needs to be
// an absolute reference
Vector2 intended_window_size;
RenderTexture2D base_canvas;
} GameEngine_t;
#define SCENE_ACTIVE_BIT (1 << 0) // Systems Active

6
main.c
View File

@ -3,6 +3,7 @@
#include "scene_impl.h"
#include "ent_impl.h"
#include "mempool.h"
#include "constants.h"
#include <stdio.h>
#include <math.h>
#define N_SCENES 4
@ -15,8 +16,8 @@ static GameEngine_t engine = {
.assets = {0}
};
const int screenWidth = 1280;
const int screenHeight = 640;
const int screenWidth = VIEWABLE_MAP_WIDTH * TILE_SIZE;
const int screenHeight = VIEWABLE_MAP_HEIGHT * TILE_SIZE;
// Maintain own queue to handle key presses
struct sc_queue_32 key_buffer;
@ -25,6 +26,7 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
init_engine(&engine, (Vector2){screenWidth, screenHeight});
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
#ifndef NDEBUG

View File

@ -32,7 +32,7 @@ static void level_scene_render_func(Scene_t* scene)
CAirTimer_t* p_air = get_component(p_ent, CAIRTIMER_T);
Sprite_t* spr = get_sprite(&scene->engine->assets, "p_bigbubble");
Vector2 air_pos = {data->game_rec.x + data->game_rec.width - 32, data->game_rec.y + data->game_rec.height - 32};
Vector2 air_pos = {data->game_rec.width - 32, data->game_rec.height - 32};
for (uint8_t i = 0; i < p_air->curr_count; i++)
{
draw_sprite(spr, 0, air_pos, 0, false);
@ -41,18 +41,14 @@ static void level_scene_render_func(Scene_t* scene)
}
}
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, 70, 12, BLACK);
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, 120, 12, BLACK);
int gui_x = 5;
sprintf(buffer, "%u %u", sc_map_size_64v(&scene->ent_manager.entities), GetFPS());
DrawText(buffer, gui_x, data->game_rec.height - 12, 12, WHITE);
print_mempool_stats(buffer);
DrawText(buffer, gui_x, 150, 12, BLACK);
DrawRectangle(0, 0, data->game_rec.width, 0, (Color){0,0,0,64});
sprintf(buffer, "Chests: %u / %u", data->coins.current, data->coins.total);
DrawText(buffer, gui_x, data->game_rec.y + data->game_rec.height, 24, BLACK);
gui_x = data->game_rec.width - MeasureText(buffer, 24) - 5;
DrawText(buffer, gui_x, 0, 24, RED);
EndTextureMode();
}
@ -488,8 +484,7 @@ void init_game_scene(LevelScene_t* scene)
init_level_scene_data(
&scene->data, MAX_N_TILES, all_tiles,
(Rectangle){
(scene->scene.engine->intended_window_size.x- VIEWABLE_MAP_WIDTH*TILE_SIZE) / 2.0f,
(scene->scene.engine->intended_window_size.y- VIEWABLE_MAP_HEIGHT*TILE_SIZE) / 2.0f,
0,0,
VIEWABLE_MAP_WIDTH*TILE_SIZE, VIEWABLE_MAP_HEIGHT*TILE_SIZE
}
);
@ -504,13 +499,9 @@ void init_game_scene(LevelScene_t* scene)
scene->data.game_rec
);
add_scene_layer(
&scene->scene, scene->scene.engine->intended_window_size.x,
scene->scene.engine->intended_window_size.y,
(Rectangle){
0, 0,
scene->scene.engine->intended_window_size.x,
scene->scene.engine->intended_window_size.y
}
&scene->scene,
scene->data.game_rec.width, scene->data.game_rec.height,
scene->data.game_rec
);
create_player(&scene->scene.ent_manager);

View File

@ -58,6 +58,7 @@ static void level_select_do_action(Scene_t* scene, ActionType_t action, bool pre
if (!pressed)
{
unsigned int prev_sel = data->scroll_area.curr_selection;
// TODO: Add scene offset to scroll area calculation
if (vert_scrollarea_set_pos(&data->scroll_area, scene->mouse_pos) != data->scroll_area.n_items)
{
vert_scrollarea_refocus(&data->scroll_area);
@ -96,18 +97,21 @@ static void level_select_do_action(Scene_t* scene, ActionType_t action, bool pre
}
}
#define START_X 300
#define START_Y 100
#define FONT_SIZE 30
#define TEXT_PADDING 3
#define DISPLAY_AREA_HEIGHT 400
#define SCROLL_TOTAL_HEIGHT 800
#define DISPLAY_AREA_HEIGHT 200
#define SCROLL_TOTAL_HEIGHT 300
void init_level_select_scene(LevelSelectScene_t* scene)
{
init_scene(&scene->scene, &level_select_do_action, 0);
add_scene_layer(
&scene->scene, 400, 800,
(Rectangle){START_X, START_Y, 400, 800}
&scene->scene, scene->scene.engine->intended_window_size.x,
scene->scene.engine->intended_window_size.y,
(Rectangle){
0, 0,
scene->scene.engine->intended_window_size.x,
scene->scene.engine->intended_window_size.y
}
);
vert_scrollarea_init(&scene->data.scroll_area, (Rectangle){50, 100, 150, DISPLAY_AREA_HEIGHT - 100}, (Vector2){150, SCROLL_TOTAL_HEIGHT});
vert_scrollarea_set_item_dims(&scene->data.scroll_area, FONT_SIZE, TEXT_PADDING);

View File

@ -3,22 +3,21 @@
#include "raymath.h"
#include <stdio.h>
#define START_X 300
static void menu_scene_render_func(Scene_t* scene)
{
MenuSceneData_t* data = &(CONTAINER_OF(scene, MenuScene_t, scene)->data);
Sprite_t* spr = get_sprite(&scene->engine->assets, "title_spr");
Rectangle render_rec = scene->layers.render_layers[0].render_area;
BeginTextureMode(scene->layers.render_layers[0].layer_tex);
ClearBackground(RAYWHITE);
DrawText("Bunny's Spelunking Adventure", START_X, 100, 32, BLACK);
int title_width = MeasureText("Bunny's Spelunking Adventure", 32);
DrawText("Bunny's Spelunking Adventure", (render_rec.width - title_width) / 2, 20, 32, BLACK);
UI_button(data->buttons, "Start");
UI_button(data->buttons + 1, "Sandbox");
UI_button(data->buttons + 2, "Continue");
UI_button(data->buttons + 3, "Exit");
UI_button(data->buttons + 1, "Continue");
UI_button(data->buttons + 2, "Exit");
draw_sprite(spr, 0, (Vector2){START_X + 200, 120}, 0, false);
draw_sprite(spr, 0, (Vector2){render_rec.width / 2, 50}, 0, false);
EndTextureMode();
}
@ -29,10 +28,7 @@ static void exec_component_function(Scene_t* scene, int sel)
case 0:
change_scene(scene->engine, LEVEL_SELECT_SCENE);
break;
case 1:
change_scene(scene->engine, SANDBOX_SCENE);
break;
case 3:
case 2:
scene->state = 0;
break;
default:
@ -143,23 +139,25 @@ void init_menu_scene(MenuScene_t* scene)
sc_array_add(&scene->scene.systems, &gui_loop);
sc_array_add(&scene->scene.systems, &menu_scene_render_func);
int button_x = scene->scene.engine->intended_window_size.x / 4;
int button_y = scene->scene.engine->intended_window_size.y / 4;
scene->data.buttons[0] = (UIComp_t) {
.bbox = {START_X,255,125,30},
.bbox = {button_x,button_y,125,30},
.state = STATE_NORMAL,
.alpha = 1.0
};
scene->data.buttons[1] = (UIComp_t) {
.bbox = {START_X,300,125,30},
.bbox = {button_x,button_y+45,125,30},
.state = STATE_NORMAL,
.alpha = 1.0
};
scene->data.buttons[2] = (UIComp_t) {
.bbox = {START_X,345,125,30},
.bbox = {button_x,button_y+90,125,30},
.state = STATE_NORMAL,
.alpha = 1.0
};
scene->data.buttons[3] = (UIComp_t) {
.bbox = {START_X,390,125,30},
.bbox = {button_x,button_y+135,125,30},
.state = STATE_NORMAL,
.alpha = 1.0
};