Skip to content

Commit 417ab3d

Browse files
committed
rg_display: update osd init function to use corners
1 parent 48b9619 commit 417ab3d

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
lines changed

components/retro-go/rg_display.c

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ static int16_t map_viewport_to_source_y[RG_SCREEN_HEIGHT + 1];
1717
static uint32_t screen_line_checksum[RG_SCREEN_HEIGHT + 1];
1818

1919
// OSD Surface Management
20-
static rg_surface_t *osd_surface = NULL;
21-
static rg_rect_t osd_rect; // Store the OSD's position and size
2220
static bool osd_enabled = false;
21+
static rg_osd_t osd;
2322

2423
#define LINE_IS_REPEATED(Y) (map_viewport_to_source_y[(Y)] == map_viewport_to_source_y[(Y) - 1])
2524
// This is to avoid flooring a number that is approximated to .9999999 and be explicit about it
@@ -42,28 +41,53 @@ static const char *SETTING_CUSTOM_ZOOM = "DispCustomZoom";
4241

4342
// TODO : make it more user-friendly -> instead of specifying a rect and a surface
4443
// just specify the corner and the dimensions (width and height)
45-
void rg_display_set_osd_surface(rg_surface_t *surface, rg_rect_t rect)
44+
rg_surface_t *rg_display_init_osd(rg_corner_t corner, int width, int height, bool has_transparency)
4645
{
47-
// Free the old surface if it exists
48-
if (osd_surface != NULL) {
49-
rg_surface_free(osd_surface);
50-
}
46+
// clipping if osd > display
47+
width = RG_MIN(width, display.screen.width);
48+
height = RG_MIN(height, display.screen.height);
49+
50+
osd.has_transparency = has_transparency;
5151

52-
osd_surface = surface;
53-
osd_rect = rect; // Store the position and size
54-
if (surface != NULL)
55-
{ // Only clear if a surface is provided
56-
uint16_t *buffer = surface->data; // Treat the buffer as 16-bit values
57-
for (int i = 0; i < surface->width * surface->height; i++)
58-
buffer[i] = C_TRANSPARENT; // Assign the black color directly // C_TRANSPARENT
52+
int left, top;
53+
switch (corner)
54+
{
55+
case CORNER_TOP_LEFT:
56+
left = 0;
57+
top = 0;
58+
break;
59+
case CORNER_TOP_RIGHT:
60+
left = display.screen.width - width;
61+
top = 0;
62+
break;
63+
case CORNER_BOTTOM_LEFT:
64+
left = 0;
65+
top = display.screen.height - height;
66+
break;
67+
case CORNER_BOTTOM_RIGHT:
68+
left = display.screen.width - width;
69+
top = display.screen.height - height;
70+
break;
71+
default:
72+
left = 0;
73+
top = 0;
74+
break;
5975
}
6076

61-
osd_enabled = (surface != NULL); // Enable OSD if surface is valid
62-
}
77+
osd.surface = rg_surface_create(width, height, RG_PIXEL_565_LE, MEM_SLOW);
6378

64-
rg_surface_t* rg_display_get_osd_surface()
65-
{
66-
return osd_surface;
79+
osd.left = left;
80+
osd.top = top;
81+
82+
if (has_transparency)
83+
{ // we fill the background with the "transparent color"
84+
uint16_t *buffer = osd.surface->data; // Treat the buffer as 16-bit values
85+
for (int i = 0; i < osd.surface->width * osd.surface->height; i++)
86+
buffer[i] = C_TRANSPARENT; // Assign the C_TRANSPARENT color directly to the background
87+
}
88+
89+
osd_enabled = 1; // Enable OSD if surface is valid
90+
return osd.surface;
6791
}
6892

6993
void rg_display_set_osd_enabled(bool enabled)
@@ -79,8 +103,7 @@ bool rg_display_is_osd_enabled()
79103

80104
void deinit_osd()
81105
{
82-
rg_surface_free(osd_surface); // Free the OSD surface
83-
osd_surface = NULL;
106+
rg_surface_free(osd.surface); // Free the OSD surface
84107
osd_enabled = false;
85108
}
86109

@@ -253,25 +276,22 @@ static inline void write_update(const rg_surface_t *update)
253276
lines_remaining -= lines_to_copy;
254277
}
255278

256-
if (osd_enabled && osd_surface != NULL)
279+
if (osd_enabled)
257280
{
258281
// TODO: Draw on screen display. By default it should be bottom left which is fine
259282
// for both virtual keyboard and info labels. Maybe make it configurable later...
260-
int *buffer = osd_surface->data;
283+
int *buffer = osd.surface->data;
261284
RG_ASSERT_ARG(buffer);
262285

263-
// Clipping
264-
int width = RG_MIN(osd_rect.width, display.screen.width - osd_rect.left);
265-
int height = RG_MIN(osd_rect.height, display.screen.height - osd_rect.top);
286+
int width = osd.surface->width;
287+
int height = osd.surface->height;
266288

267-
// This can happen when left or top is out of bound
268-
if (width < 0 || height < 0)
269-
return;
289+
int top = osd.top;
290+
int left = osd.left;
270291

271-
lcd_set_window(osd_rect.left + display.screen.margin_left, osd_rect.top + display.screen.margin_top, width, height);
292+
lcd_set_window(osd.left + display.screen.margin_left, osd.top + display.screen.margin_top, width, height);
272293

273294
// TODO : find a way to get the background pixels
274-
// TODO : only draw the osd when the "background surface" has changed
275295
for (size_t y = 0; y < height;)
276296
{
277297
uint16_t *lcd_buffer = lcd_get_buffer(LCD_BUFFER_LENGTH);
@@ -280,7 +300,7 @@ static inline void write_update(const rg_surface_t *update)
280300
// Copy line by line because stride may not match width
281301
for (size_t line = 0; line < num_lines; ++line)
282302
{
283-
uint16_t *src = (void *)buffer + ((y + line) * osd_rect.width * 2);
303+
uint16_t *src = (void *)buffer + ((y + line) * osd.surface->width * 2);
284304
uint16_t *dst = lcd_buffer + (line * width);
285305
for (size_t i = 0; i < width; ++i){
286306
if(src[i] != C_TRANSPARENT) // only overwrite pixels that aren't transparent

components/retro-go/rg_display.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,22 @@ typedef struct
9999

100100
#include "rg_surface.h"
101101

102-
void rg_display_set_osd_surface(rg_surface_t *surface, rg_rect_t rect);
103-
rg_surface_t* rg_display_get_osd_surface();
102+
typedef struct
103+
{
104+
rg_surface_t *surface;
105+
bool has_transparency;
106+
int top, left;
107+
} rg_osd_t;
108+
109+
typedef enum
110+
{
111+
CORNER_TOP_LEFT = 0,
112+
CORNER_TOP_RIGHT,
113+
CORNER_BOTTOM_LEFT,
114+
CORNER_BOTTOM_RIGHT
115+
} rg_corner_t;
116+
117+
rg_surface_t *rg_display_init_osd(rg_corner_t corner, int width, int height, bool has_transparency);
104118
void rg_display_set_osd_enabled(bool enabled);
105119
bool rg_display_is_osd_enabled();
106120
void deinit_osd();

components/retro-go/rg_system.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,8 @@ static void system_monitor_task(void *arg)
247247
rg_surface_t *osd = NULL;
248248
if(!app.isLauncher) // not working
249249
{
250-
osd = rg_surface_create(280, 20, RG_PIXEL_565_LE, MEM_SLOW);
251-
252-
// Create a rect to store the osd position and size
253-
rg_rect_t osd_rect = {0, 0, 280, 20}; // Example: top-left corner
254-
255-
// Set the OSD surface and position
256-
rg_display_set_osd_surface(osd, osd_rect);
250+
// get the OSD surface
251+
osd = rg_display_init_osd(CORNER_TOP_LEFT, 280, 15, 1);
257252
}
258253

259254
int64_t nextLoopTime = 0;
@@ -280,7 +275,6 @@ static void system_monitor_task(void *arg)
280275
rg_gui_draw_status_bars_osd(osd);
281276
rg_gui_battery_indicator(osd);
282277
}
283-
284278

285279
// Try to avoid complex conversions that could allocate, prefer rounding/ceiling if necessary.
286280
rg_system_log(RG_LOG_DEBUG, NULL, "STACK:%d, HEAP:%d+%d (%d+%d), BUSY:%d%%, FPS:%d (%d+%d+%d), BATT:%d\n",

retro-core/components/snes9x/src/memmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ void InitROM(bool Interleaved)
609609
Sanitize(Memory.ROMId, sizeof(Memory.ROMId));
610610
Sanitize(Memory.CompanyId, sizeof(Memory.CompanyId));
611611

612-
printf("Rom loaded: name: %s, id: %s, company: %s, size: %dKB\n", Memory.ROMName, Memory.ROMId, Memory.CompanyId, Memory.CalculatedSize / 1024);
612+
printf("Rom loaded: name: %s, id: %s, company: %s, size: %ldKB\n", Memory.ROMName, Memory.ROMId, Memory.CompanyId, Memory.CalculatedSize / 1024);
613613
Settings.ForceHeader = Settings.ForceHiROM = Settings.ForceLoROM = Settings.ForceInterleaved = Settings.ForceNoHeader = Settings.ForceNotInterleaved = Settings.ForceInterleaved2 = false;
614614
}
615615

0 commit comments

Comments
 (0)