Skip to content

Commit c9f067c

Browse files
committed
adding blacks bars , adding ui module , adding window bindings , adding correction to offscreen
1 parent e280b82 commit c9f067c

File tree

18 files changed

+257
-30
lines changed

18 files changed

+257
-30
lines changed

grngame/bindings/da_script_engine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "grngame/audio/speech.h"
99
#include "grngame/bindings/renderer_module.hpp"
1010
#include "grngame/bindings/sound_module.hpp"
11+
#include "grngame/bindings/window_module.hpp"
1112
#include "grngame/dev/logging.h"
1213
#include "grngame/platform/directories.h"
1314
#include <filesystem>
@@ -23,6 +24,7 @@ DaScriptEngine::DaScriptEngine() : file_access(das::make_smart<das::FsFileAccess
2324

2425
NEED_MODULE(RendererModule);
2526
NEED_MODULE(SoundModule);
27+
NEED_MODULE(WindowModule);
2628
NEED_MODULE(Module_dasSQLITE);
2729
}
2830

grngame/bindings/window_module.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "window_module.hpp"
2+
3+
extern "C"
4+
{
5+
#include "grngame/core/app.h"
6+
#include "grngame/core/window.h"
7+
}
8+
9+
static void window_fullscreen()
10+
{
11+
WindowFullscreen(&g_app.info);
12+
}
13+
14+
static void window_maximized()
15+
{
16+
WindowMaximized(&g_app.info);
17+
}
18+
19+
static void window_set_size(int width, int height)
20+
{
21+
WindowSetSize(&g_app.info, (uint16)width, (uint16)height);
22+
}
23+
24+
static int window_get_width()
25+
{
26+
return (int)WindowGetWidth();
27+
}
28+
29+
static int window_get_height()
30+
{
31+
return (int)WindowGetHeight();
32+
}
33+
34+
static int window_get_universe_width()
35+
{
36+
return (int)WindowGetUniverseWidth();
37+
}
38+
39+
static int window_get_universe_height()
40+
{
41+
return (int)WindowGetUniverseHeight();
42+
}
43+
44+
static float window_get_offset_x()
45+
{
46+
return WindowGetOffsetX();
47+
}
48+
49+
static float window_get_offset_y()
50+
{
51+
return WindowGetOffsetY();
52+
}
53+
54+
// todo make c function
55+
static bool window_is_force_universe_scale()
56+
{
57+
return g_app.info.force_universe_scale;
58+
}
59+
60+
WindowModule::WindowModule() : Module("grngame_window")
61+
{
62+
auto module_library = das::ModuleLibrary(this);
63+
module_library.addBuiltInModule();
64+
module_library.addModule(this);
65+
66+
das::addExtern<DAS_BIND_FUN(window_fullscreen)>(*this, module_library, "window_fullscreen",
67+
das::SideEffects::modifyExternal, "window_fullscreen");
68+
das::addExtern<DAS_BIND_FUN(window_maximized)>(*this, module_library, "window_maximized",
69+
das::SideEffects::modifyExternal, "window_maximized");
70+
das::addExtern<DAS_BIND_FUN(window_set_size)>(*this, module_library, "window_set_size",
71+
das::SideEffects::modifyExternal, "window_set_size");
72+
73+
das::addExtern<DAS_BIND_FUN(window_get_width)>(*this, module_library, "window_get_width",
74+
das::SideEffects::modifyExternal, "window_get_width");
75+
das::addExtern<DAS_BIND_FUN(window_get_height)>(*this, module_library, "window_get_height",
76+
das::SideEffects::modifyExternal, "window_get_height");
77+
das::addExtern<DAS_BIND_FUN(window_get_universe_width)>(*this, module_library, "window_get_universe_width",
78+
das::SideEffects::modifyExternal,
79+
"window_get_universe_width");
80+
das::addExtern<DAS_BIND_FUN(window_get_universe_height)>(*this, module_library, "window_get_universe_height",
81+
das::SideEffects::modifyExternal,
82+
"window_get_universe_height");
83+
das::addExtern<DAS_BIND_FUN(window_get_offset_x)>(*this, module_library, "window_get_offset_x",
84+
das::SideEffects::modifyExternal, "window_get_offset_x");
85+
das::addExtern<DAS_BIND_FUN(window_get_offset_y)>(*this, module_library, "window_get_offset_y",
86+
das::SideEffects::modifyExternal, "window_get_offset_y");
87+
das::addExtern<DAS_BIND_FUN(window_is_force_universe_scale)>(
88+
*this, module_library, "window_is_force_universe_scale", das::SideEffects::modifyExternal,
89+
"window_is_force_universe_scale");
90+
}
91+
92+
REGISTER_MODULE(WindowModule);

grngame/bindings/window_module.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include "da_script_engine.hpp"
3+
4+
class WindowModule : public das::Module
5+
{
6+
public:
7+
WindowModule();
8+
};

grngame/core/app.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ static void MainLoop()
107107

108108
RendererClear(&g_app.renderer);
109109
DaScriptManagerCallOnRender(g_app.da_script);
110+
ApplyBlackStripes();
110111
RendererPresent(&g_app.renderer);
111112

112113
Uint64 frame_end = SDL_GetTicks();

grngame/core/app.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ typedef struct
3333
bool enable_logs;
3434
LogDestination log_destination;
3535

36+
bool force_universe_scale; // apply black stripes to the window
37+
3638
// for draw render
3739
uint8 r, g, b;
3840
} AppInfo;

grngame/core/window.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "app.h"
66
#include "cglm/types-struct.h"
77
#include "grngame/dev/logging.h"
8+
#include "grngame/renderer/renderer.h"
89
#include "grngame/utils/attributes.h"
910

1011
// sdl wrappent
@@ -81,6 +82,36 @@ uint8 WindowGetScale()
8182
return (uint8)c;
8283
}
8384

85+
int WindowGetWidth()
86+
{
87+
return (int)g_app.info.window_width;
88+
}
89+
90+
int WindowGetHeight()
91+
{
92+
return (int)g_app.info.window_height;
93+
}
94+
95+
int WindowGetUniverseWidth()
96+
{
97+
return (int)g_app.info.window_universe_width;
98+
}
99+
100+
int WindowGetUniverseHeight()
101+
{
102+
return (int)g_app.info.window_universe_height;
103+
}
104+
105+
float32 WindowGetOffsetX()
106+
{
107+
return g_app.info.offset_x;
108+
}
109+
110+
float32 WindowGetOffsetY()
111+
{
112+
return g_app.info.offset_y;
113+
}
114+
84115
void ApplyResizing(AppInfo *app_info, int16 width, int16 height)
85116
{
86117
int8 coeff_w = width / (int16)app_info->window_universe_width;
@@ -152,4 +183,27 @@ void WindowSetSize(AppInfo *app_info, uint16 width, uint16 height)
152183
ApplyResizing(app_info, width, height);
153184
app_info->window_fullscreen = false;
154185
app_info->window_maximised = false;
155-
}
186+
}
187+
188+
void ApplyBlackStripes()
189+
{
190+
if (g_app.info.force_universe_scale)
191+
{
192+
float32 off_x = g_app.info.offset_x;
193+
float32 off_y = g_app.info.offset_y;
194+
float32 uw = (float32)g_app.info.window_universe_width;
195+
float32 uh = (float32)g_app.info.window_universe_height;
196+
float32 total_w = uw + 2.0f * off_x;
197+
float32 total_h = uh + 2.0f * off_y;
198+
199+
SDL_FRect rects[4] = {
200+
{0.0f, 0.0f, off_x, total_h},
201+
{off_x + uw, 0.0f, off_x, total_h},
202+
{0.0f, 0.0f, total_w, off_y},
203+
{0.0f, off_y + uh, total_w, off_y},
204+
};
205+
206+
RendererSetColor(10, 10, 10, 255);
207+
RendererFillRects(rects, 4);
208+
}
209+
}

grngame/core/window.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ void ApplyResizing(AppInfo *app_info, int16 width, int16 height);
1313
void WindowFullscreen(AppInfo *app_info);
1414
void WindowMaximized(AppInfo *app_info);
1515
void WindowSetSize(AppInfo *app_info, uint16 width, uint16 height);
16-
uint8 WindowGetScale();
16+
uint8 WindowGetScale();
17+
int WindowGetWidth();
18+
int WindowGetHeight();
19+
int WindowGetUniverseWidth();
20+
int WindowGetUniverseHeight();
21+
float32 WindowGetOffsetX();
22+
float32 WindowGetOffsetY();
23+
void ApplyBlackStripes();

grngame/renderer/renderer.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bool RendererTryCreate(SDL_Window *window, Renderer *renderer)
1919

2020
void RendererClear(const Renderer *renderer)
2121
{
22-
SDL_SetRenderDrawColor(renderer->renderer, g_app.info.r, g_app.info.g, g_app.info.b, 255);
22+
RendererSetColor(g_app.info.r, g_app.info.g, g_app.info.b, 255);
2323
if (UNLIKELY(!SDL_RenderClear(renderer->renderer)))
2424
LOG_ERROR("Failed to clear renderer: %s", SDL_GetError());
2525
}
@@ -74,10 +74,12 @@ bool OffScreen(float32 x, float32 y, float32 w, float32 h)
7474
float32 view_w = (float32)g_app.info.window_universe_width;
7575
float32 view_h = (float32)g_app.info.window_universe_height;
7676

77-
float32 sx = x + g_app.info.offset_x;
78-
float32 sy = y + g_app.info.offset_y;
77+
if (g_app.info.force_universe_scale)
78+
return (x + w <= 0.0f) || (x >= view_w) || (y + h <= 0.0f) || (y >= view_h);
7979

80-
return (sx + w <= 0.0f) || (sx >= view_w) || (sy + h <= 0.0f) || (sy >= view_h);
80+
float32 off_x = g_app.info.offset_x;
81+
float32 off_y = g_app.info.offset_y;
82+
return (x + w <= -off_x) || (x >= view_w + off_x) || (y + h <= -off_y) || (y >= view_h + off_y);
8183
}
8284

8385
void SetRenderColor(uint8 r, uint8 g, uint8 b)

grngame/renderer/sprite.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bool SpriteDraw(Sprite s, uint16 frame, float x, float y, uint8 c, bool f, int16
2222
if (OffScreen(x, y, tex_w, tex->h))
2323
{
2424
// todo log
25+
LOG_ERROR("impossible");
2526
return false;
2627
}
2728

grngame/renderer/texture.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ bool TextureDraw(const char *name, float x, float y, uint8 c, bool f, int16 r, u
2626
if (OffScreen(x, y, tex->w, tex->h))
2727
{
2828
// todo log
29+
LOG_ERROR("impossible");
2930
return false;
3031
}
3132

0 commit comments

Comments
 (0)