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 8 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
5 changes: 3 additions & 2 deletions src/render/ogc/SDL_render_ogc.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "../../video/ogc/SDL_ogcgxcommon.h"
#include "../../video/ogc/SDL_ogcpixels.h"
#include "../../video/ogc/SDL_ogcvideo.h"
#include <ogc/conf.h>

#include <malloc.h>
#include <ogc/cache.h>
Expand Down Expand Up @@ -426,8 +427,8 @@ static int OGC_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL
static int OGC_RenderSetViewPort(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
const SDL_Rect *viewport = &cmd->data.viewport.rect;

OGC_set_viewport(viewport->x, viewport->y, viewport->w, viewport->h);
OGC_set_viewport(viewport->x, viewport->y, viewport->w, viewport->h,
viewport->w==640 ? (viewport->h*16.0f/9.0f)+1 : viewport->w);
Fancy2209 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

Expand Down
15 changes: 8 additions & 7 deletions src/video/ogc/SDL_ogcgxcommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,24 @@ static const f32 tex_pos[] __attribute__((aligned(32))) = {
1.0,
};

void OGC_set_viewport(int x, int y, int w, int h)
void OGC_set_viewport(int x, int y, int w, int h, int orthoWidth)
mardy marked this conversation as resolved.
Show resolved Hide resolved
{
Mtx44 proj;
SDL_Log("%d %d %d %d %d", x, y, w, h, orthoWidth);

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);
mardy marked this conversation as resolved.
Show resolved Hide resolved

// matrix, t, b, l, r, n, f
guOrtho(proj, 0, h, 0, w, 0, 1);
guOrtho(proj, 0, h, 0, orthoWidth, 0, 1);
GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
}

void OGC_draw_init(int w, int h)
void OGC_draw_init(int w, int h, int orthoWidth)
{
Mtx mv;

SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "OGC_draw_init called with %d, %d", w, h);
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "OGC_draw_init called with %d, %d, %d", w, h, orthoWidth);

guMtxIdentity(mv);
/* Ideally we would use 0.5 to center the coordinates on the pixels, but
Expand Down Expand Up @@ -86,7 +87,7 @@ void OGC_draw_init(int w, int h)
GX_SetTevOp(GX_TEVSTAGE0, GX_REPLACE);
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);

OGC_set_viewport(0, 0, w, h);
OGC_set_viewport(0, 0, w, h, orthoWidth);

GX_InvVtxCache(); // update vertex cache
}
Expand Down
4 changes: 2 additions & 2 deletions src/video/ogc/SDL_ogcgxcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

#define GX_COLOR_AS_U32(c) *((u32*)&c)

void OGC_draw_init(int w, int h);
void OGC_set_viewport(int x, int y, int w, int h);
void OGC_draw_init(int w, int h, int orthoWidth);
void OGC_set_viewport(int x, int y, int w, int h, int orthoWidth);
void OGC_load_texture(void *texels, int w, int h, u8 gx_format,
SDL_ScaleMode scale_mode);

Expand Down
12 changes: 8 additions & 4 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 @@ -175,7 +176,7 @@ void OGC_draw_cursor(_THIS)
SDL_Mouse *mouse = SDL_GetMouse();
OGC_CursorData *curdata;
Mtx mv;
int screen_w, screen_h;
int screen_w, screen_h, viewport_w;
float angle = 0.0f;

if (!mouse || !mouse->cursor_shown ||
Expand All @@ -191,7 +192,8 @@ void OGC_draw_cursor(_THIS)
if (!data->ir.valid) return;
}

screen_w = _this->displays[0].current_mode.w;
viewport_w = _this->displays[0].current_mode.w;
screen_w = (viewport_w == 854) ? 640 : _this->displays[0].current_mode.w;
mardy marked this conversation as resolved.
Show resolved Hide resolved
screen_h = _this->displays[0].current_mode.h;

curdata = mouse->cur_cursor->driverdata;
Expand All @@ -200,6 +202,7 @@ void OGC_draw_cursor(_THIS)

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

if (angle != 0.0f) {
Mtx rot;
guMtxRotDeg(rot, 'z', angle);
Expand All @@ -208,7 +211,7 @@ void OGC_draw_cursor(_THIS)
guMtxTransApply(mv, mv, mouse->x, mouse->y, 0);
GX_LoadPosMtxImm(mv, GX_PNMTX1);

OGC_set_viewport(0, 0, screen_w, screen_h);
OGC_set_viewport(0, 0, screen_w, screen_h, viewport_w);

GX_ClearVtxDesc();
GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
Expand Down Expand Up @@ -240,7 +243,8 @@ void OGC_draw_cursor(_THIS)
SDL_Renderer *renderer = SDL_GetRenderer(_this->windows);
if (renderer) {
OGC_set_viewport(renderer->viewport.x, renderer->viewport.y,
renderer->viewport.w, renderer->viewport.h);
renderer->viewport.w, renderer->viewport.h,
renderer->viewport.w);
}
}
}
Expand Down
73 changes: 64 additions & 9 deletions src/video/ogc/SDL_ogcvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <ogc/gx.h>
#include <ogc/system.h>
#include <ogc/video.h>
#include <ogc/conf.h>

#include <opengx.h>

Expand All @@ -51,6 +52,7 @@

/* A video mode with a 320 width; we'll build it programmatically. */
static GXRModeObj s_mode320;
static GXRModeObj s_mode704;

static const GXRModeObj *s_ntsc_modes[] = {
&TVNtsc240Ds,
Expand Down Expand Up @@ -85,14 +87,14 @@ static const GXRModeObj *s_pal_modes[] = {
static int OGC_VideoInit(_THIS);
static void OGC_VideoQuit(_THIS);

static void init_display_mode(SDL_DisplayMode *mode, const GXRModeObj *vmode)
static void init_display_mode(SDL_DisplayMode *mode, const GXRModeObj *vmode, int width)
mardy marked this conversation as resolved.
Show resolved Hide resolved
{
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->w = width;
mode->h = vmode->efbHeight;
switch (format) {
case VI_DEBUG:
Expand Down Expand Up @@ -142,18 +144,48 @@ static void add_supported_modes(SDL_VideoDisplay *display, u32 tv_format)
* take care of the horizontal scale for us. */
memcpy(&s_mode320, gx_modes[0], sizeof(s_mode320));
s_mode320.fbWidth = 320;
init_display_mode(&mode, &s_mode320);
init_display_mode(&mode, &s_mode320, s_mode320.fbWidth);
SDL_AddDisplayMode(display, &mode);

// libogc uses 640 for the viWidth, but this does not work well for Widescreen
// as such we extend the viWidth to 704 on a new videomode
// TODO: Currently ony added on wii if it's widescreen,
// but should work on GC and a 4:3 Wii
// testing needed

#ifdef __wii__
if(CONF_GetAspectRatio() == CONF_ASPECT_16_9)
{
memcpy(&s_mode704, gx_modes[1], sizeof(s_mode704));
s_mode704.viWidth = 704;

// set Center point
if (&s_mode704 == &TVPal576IntDfScale || &s_mode704 == &TVPal576ProgScale)
{
s_mode704.viXOrigin = (VI_MAX_WIDTH_PAL - s_mode704.viWidth) / 2;
s_mode704.viYOrigin = (VI_MAX_HEIGHT_PAL - s_mode704.viHeight) / 2;
}
else
{
s_mode704.viXOrigin = (VI_MAX_WIDTH_NTSC - s_mode704.viWidth) / 2;
s_mode704.viYOrigin = (VI_MAX_HEIGHT_NTSC - s_mode704.viHeight) / 2;
}

// Widescreen is anamorphic, so we provide a different width for the ortho projection
init_display_mode(&mode, &s_mode704, 854);
SDL_AddDisplayMode(display, &mode);
}
#endif

/* Now add all the "standard" modes from libogc */
while (*gx_modes) {
init_display_mode(&mode, *gx_modes);
init_display_mode(&mode, *gx_modes, (*gx_modes)->fbWidth);
SDL_AddDisplayMode(display, &mode);
gx_modes++;
}
}

static void setup_video_mode(_THIS, GXRModeObj *vmode)
static void setup_video_mode(_THIS, GXRModeObj *vmode, int orthoWidth)
{
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;

Expand All @@ -180,7 +212,7 @@ static void setup_video_mode(_THIS, GXRModeObj *vmode)
GX_SetFieldMode(vmode->field_rendering,
((vmode->viHeight == 2 * vmode->xfbHeight) ? GX_ENABLE : GX_DISABLE));

OGC_draw_init(vmode->fbWidth, vmode->efbHeight);
OGC_draw_init(vmode->fbWidth, vmode->efbHeight, orthoWidth);
}

static int OGC_SetDisplayMode(_THIS, SDL_VideoDisplay *display,
Expand All @@ -195,7 +227,7 @@ static int OGC_SetDisplayMode(_THIS, SDL_VideoDisplay *display,
if (videodata->xfb[1])
free(MEM_K1_TO_K0(videodata->xfb[1]));

setup_video_mode(_this, vmode);
setup_video_mode(_this, vmode, mode->w);
return 0;
}

Expand Down Expand Up @@ -277,12 +309,30 @@ int OGC_VideoInit(_THIS)
VIDEO_Init();

vmode = VIDEO_GetPreferredMode(NULL);
vmode->viWidth = 704;

// set Center point
if (&s_mode704 == &TVPal576IntDfScale || &s_mode704 == &TVPal576ProgScale)
{
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);
GX_Init(videodata->gp_fifo, DEFAULT_FIFO_SIZE);

setup_video_mode(_this, vmode);
#ifdef __wii__
if(CONF_GetAspectRatio() == CONF_ASPECT_16_9)
setup_video_mode(_this, vmode, 854);
else
#endif
setup_video_mode(_this, vmode, vmode->fbWidth);
GX_SetCopyClear(background, GX_MAX_Z24);

GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
Expand All @@ -293,7 +343,12 @@ int OGC_VideoInit(_THIS)

GX_Flush();

init_display_mode(&mode, vmode);
#ifdef __wii__
if(CONF_GetAspectRatio() == CONF_ASPECT_16_9)
init_display_mode(&mode, vmode, 854);
else
#endif
init_display_mode(&mode, vmode, vmode->fbWidth);
if (SDL_AddBasicVideoDisplay(&mode) < 0) {
return -1;
}
Expand Down