Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OGC] Add support for Custom Aspect Ratios #91

Open
wants to merge 26 commits into
base: ogc-sdl-2.28
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8eaebcf
Adds WIP Widescreen Support
Fancy2209 Feb 4, 2025
37151df
Fix Cursor
Fancy2209 Feb 4, 2025
62191d5
Add orthoWidth to draw init log
Fancy2209 Feb 4, 2025
19e2688
Delete accidentally commited file
Fancy2209 Feb 4, 2025
e1f2bfd
Fix orthoWidth in Renderer
Fancy2209 Feb 5, 2025
6570d34
Add missing include
Fancy2209 Feb 5, 2025
4a73bb7
Remove resizing from Renderer, doesn't work properly
Fancy2209 Feb 5, 2025
fc69931
Fix SDL Renderer and OGC_set_viewport
Fancy2209 Feb 5, 2025
ab85398
Remove debug logging
Fancy2209 Feb 5, 2025
d6efcad
Update src/render/ogc/SDL_render_ogc.c
Fancy2209 Feb 5, 2025
4dc34db
Apply reviews
Fancy2209 Feb 5, 2025
7afc872
Merge branch 'Widescreen' of https://github.com/Fancy2209/SDL into Wi…
Fancy2209 Feb 5, 2025
016de5f
Close comment
Fancy2209 Feb 5, 2025
a5d6187
Expand comment
Fancy2209 Feb 5, 2025
7fc3f5c
Fix build
Fancy2209 Feb 5, 2025
052e287
Fix Widescreen Vide Mode Width Calculation
Fancy2209 Feb 5, 2025
027d5d2
Fix mouse Widescreen Viewport
Fancy2209 Feb 5, 2025
5219b8f
Revert ogcmouse changes, simply swap the names of screen_w and viewpo…
Fancy2209 Feb 5, 2025
d1b37b4
Assorted Fixes
Fancy2209 Feb 5, 2025
412eb51
Delete .vscode
Fancy2209 Feb 5, 2025
e254a96
Better region check when setting the video mode center point
Fancy2209 Feb 6, 2025
072708b
Make code much cleaner
Fancy2209 Feb 7, 2025
95c62a7
Merge branch 'Widescreen' of https://github.com/Fancy2209/SDL into Wi…
Fancy2209 Feb 7, 2025
40e5e47
Remove unused header
Fancy2209 Feb 7, 2025
984d25b
Add SDL_OGC_ASPECT_RATIO
Fancy2209 Feb 7, 2025
ad259a7
Make OGC_get_aspect_ratio return a float and scale the mouse only bas…
Fancy2209 Feb 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/video/ogc/SDL_ogcgxcommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <ogc/cache.h>
#include <ogc/gx.h>
#include <ogc/video.h>
#include <ogc/conf.h>

static const f32 tex_pos[] __attribute__((aligned(32))) = {
0.0,
Expand All @@ -40,12 +41,28 @@ static const f32 tex_pos[] __attribute__((aligned(32))) = {
1.0,
};

float OGC_get_aspect_ratio()
{
const char *ratioString;
float w, h;
ratioString = getenv("SDL_OGC_ASPECT_RATIO");

if(ratioString == NULL) {
#ifdef __wii__
if(CONF_GetAspectRatio() == CONF_ASPECT_16_9)
return 16.0f / 9.0f;
#endif
return 4.0f / 3.0f;
} else if(sscanf(ratioString, "%f:%f", w, h) == 2)
return w/h;
}

void OGC_set_viewport(int x, int y, int w, int h)
{
Mtx44 proj;

GX_SetViewport(x, y, w, h, 0, 1);
GX_SetScissor(x, y, w, h);
GX_SetViewport(x, y, (w > 640) ? 640 : w, h, 0, 1);
GX_SetScissor(x, y, (w > 640) ? 640 : w, h);

// matrix, t, b, l, r, n, f
guOrtho(proj, 0, h, 0, w, 0, 1);
Comment on lines +64 to 68
Copy link
Collaborator

@mardy mardy Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that GX_SetViewport and GX_SetScissor can be set to the EFB size (that is, we can consider w and h to be in pixels), and if we want to play with the aspect ratio we can play with the width parameter passed to guOrtho.

In other words, I would set leave the calls to GX_SetViewport and GX_SetScissor exactly as they were, then call OGC_get_aspect_ratio_dimensions right from within this function and call

const float ratio4_3 = 4.0f / 3.0f;
guOrtho(proj, 0, h, 0, w * ratio / ratio4_3, 0, 1); 

Of course, this needs to be checked. :-) The thing is, if you pass the same values to both functions (GX_SetViewport and guOrtho) you get a 1:1 mapping, which is not what we want for widescreen cases (or for any case where the ratio is not 4/3).

Copy link
Author

@Fancy2209 Fancy2209 Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave up aspect correcting apps that aren't using 16:9
Fixing this here would cause issues, as I've showed before it makes VVVVVVV less wide than it should.
The whole guOrtho thing being a new var really felt like a hack, and I think leaving it as is is the best approach, especially since some apps resize the viewport twice every frame and it will distort the image more than it should if we do it

Expand Down
1 change: 1 addition & 0 deletions src/video/ogc/SDL_ogcgxcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#define GX_COLOR_AS_U32(c) *((u32*)&c)

void OGC_draw_init(int w, int h);
float OGC_get_aspect_ratio();
void OGC_set_viewport(int x, int y, int w, int h);
void OGC_load_texture(void *texels, int w, int h, u8 gx_format,
SDL_ScaleMode scale_mode);
Expand Down
14 changes: 11 additions & 3 deletions src/video/ogc/SDL_ogcmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "SDL_ogcgxcommon.h"
#include "SDL_ogcmouse.h"
#include "SDL_ogcpixels.h"
#include "SDL_ogcvideo.h"

#include "../SDL_sysvideo.h"
#include "../../render/SDL_sysrender.h"
Expand Down Expand Up @@ -178,6 +179,8 @@ void OGC_draw_cursor(_THIS)
int screen_w, screen_h;
float angle = 0.0f;

float aspect_ratio = OGC_get_aspect_ratio();

if (!mouse || !mouse->cursor_shown ||
!mouse->cur_cursor || !mouse->cur_cursor->driverdata) {
return;
Expand All @@ -195,11 +198,16 @@ void OGC_draw_cursor(_THIS)
screen_h = _this->displays[0].current_mode.h;

curdata = mouse->cur_cursor->driverdata;
OGC_load_texture(curdata->texels, curdata->w, curdata->h, GX_TF_RGBA8,
SDL_ScaleModeNearest);
if (aspect_ratio != 4.0f/3.0f)
OGC_load_texture(curdata->texels, curdata->w, curdata->h, GX_TF_RGBA8,
SDL_ScaleModeLinear);
else
OGC_load_texture(curdata->texels, curdata->w, curdata->h, GX_TF_RGBA8,
SDL_ScaleModeNearest);

guMtxIdentity(mv);
guMtxScaleApply(mv, mv, screen_w / 640.0f, screen_h / 480.0f, 1.0f);
guMtxScaleApply(mv, mv, screen_h / 480.0f, screen_h / 480.0f, 1.0f);

if (angle != 0.0f) {
Mtx rot;
guMtxRotDeg(rot, 'z', angle);
Expand Down
22 changes: 20 additions & 2 deletions src/video/ogc/SDL_ogcvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include <malloc.h>
#include <ogc/color.h>
#include <ogc/conf.h>
#include <ogc/gx.h>
#include <ogc/system.h>
#include <ogc/video.h>
Expand Down Expand Up @@ -87,13 +88,20 @@ static void OGC_VideoQuit(_THIS);

static void init_display_mode(SDL_DisplayMode *mode, const GXRModeObj *vmode)
{
float aspect = OGC_get_aspect_ratio();

u32 format = VI_FORMAT_FROM_MODE(vmode->viTVMode);

/* Use a fake 32-bpp desktop mode */
SDL_zero(*mode);
mode->format = SDL_PIXELFORMAT_ARGB8888;
mode->w = vmode->fbWidth;

mode->h = vmode->efbHeight;
if (aspect != 4.0f/3.0f) {
mode->w = mode->h * aspect;
mode->w = (mode->w + 1) & ~1;
} else
mode->w = vmode->fbWidth;
switch (format) {
case VI_DEBUG:
case VI_NTSC:
Expand Down Expand Up @@ -170,7 +178,8 @@ static void setup_video_mode(_THIS, GXRModeObj *vmode)
VIDEO_Flush();

VIDEO_WaitVSync();
if (vmode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync();
if (vmode->viTVMode & VI_NON_INTERLACE)
VIDEO_WaitVSync();

/* Setup the EFB -> XFB copy operation */
GX_SetDispCopySrc(0, 0, vmode->fbWidth, vmode->efbHeight);
Expand Down Expand Up @@ -277,6 +286,15 @@ int OGC_VideoInit(_THIS)
VIDEO_Init();

vmode = VIDEO_GetPreferredMode(NULL);
vmode->viWidth = VI_MAX_WIDTH_NTSC; // VI_MAX_WIDTH is the same for all regions
// set Center point
if (VI_FORMAT_FROM_MODE(vmode->viTVMode) == VI_PAL) {
vmode->viXOrigin = (VI_MAX_WIDTH_PAL - vmode->viWidth) / 2;
vmode->viYOrigin = (VI_MAX_HEIGHT_PAL - vmode->viHeight) / 2;
} else {
vmode->viXOrigin = (VI_MAX_WIDTH_NTSC - vmode->viWidth) / 2;
vmode->viYOrigin = (VI_MAX_HEIGHT_NTSC - vmode->viHeight) / 2;
}

videodata->gp_fifo = memalign(32, DEFAULT_FIFO_SIZE);
memset(videodata->gp_fifo, 0, DEFAULT_FIFO_SIZE);
Expand Down