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

(WIP) First, bad, groundwork implementation of OpenGL backend #183

Merged
merged 5 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ ifeq ($(PLATFORM),sdl)
else ifeq ($(PLATFORM),sdl_win32)
@cd $(OBJ_DIR) && $(CC1) -mwin32 $(OBJS_REL) -lmingw32 -L$(ROOT_DIR)/$(SDL_MINGW_LIB) -lSDL2main -lSDL2.dll -lwinmm -lkernel32 -lxinput -o $(ROOT_DIR)/$@ -Xlinker -Map "$(ROOT_DIR)/$(MAP)"
else
@cd $(OBJ_DIR) && $(CC1) -mwin32 $(OBJS_REL) -L$(ROOT_DIR)/libagbsyscall -lagbsyscall -lkernel32 -lgdi32 -o $(ROOT_DIR)/$@ -Xlinker -Map "$(ROOT_DIR)/$(MAP)"
@cd $(OBJ_DIR) && $(CC1) -mwin32 $(OBJS_REL) -L$(ROOT_DIR)/libagbsyscall -lagbsyscall -lkernel32 -lgdi32 -lwinmm -lxinput -lopengl32 -o $(ROOT_DIR)/$@ -Xlinker -Map "$(ROOT_DIR)/$(MAP)"
endif
endif

Expand Down
11 changes: 9 additions & 2 deletions graphics.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ tileset_%.4bpp: tileset_%.png
tiles.8bpp: tiles.png
$(GFX) $< $@ -ignore_trailing

ifeq ($(PLATFORM),win32)
# For experimental OpenGL renderer
GFX_CVT_FLAGS :=
else
GFX_CVT_FLAGS := -split_into_oam_shapes
endif

graphics/obj_tiles/4bpp/%.4bpp: graphics/obj_tiles/4bpp/%.png
$(GFX) $< $@ -split_into_oam_shapes
$(GFX) $< $@ $(GFX_CVT_FLAGS)

graphics/obj_tiles/8bpp/%.8bpp: graphics/obj_tiles/8bpp/%.png
$(GFX) $< $@ -split_into_oam_shapes
$(GFX) $< $@ $(GFX_CVT_FLAGS)
10 changes: 10 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@
#define TAS_TESTING 0
#define TAS_TESTING_WIDESCREEN_HACK 1

#define RENDERER_SOFTWARE 0
#define RENDERER_OPENGL 1
#define RENDERER_COUNT 2
#if PLATFORM_WIN32 && !PLATFORM_SDL
// TODO: Only win32 for now
#define RENDERER RENDERER_OPENGL
#else
#define RENDERER RENDERER_SOFTWARE
#endif

#endif // GUARD_SA2_CONFIG_H
8 changes: 8 additions & 0 deletions include/platform/shared/input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef GUARD_PLATFORM_SHARED_INPUT_H
#define GUARD_PLATFORM_SHARED_INPUT_H

#include "sprite.h" // for Sprite

u16 GetXInputKeys();

#endif // GUARD_PLATFORM_SHARED_INPUT_H
10 changes: 10 additions & 0 deletions include/platform/shared/opengl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef GUARD_PLATFORM_SHARED_OPENGL_H
#define GUARD_PLATFORM_SHARED_OPENGL_H

#include "sprite.h" // for Sprite

void OpenGL_OnInit();
void OpenGL_DisplaySprite(Sprite *sprite, u8 oamPaletteNum);
void OpenGL_Render(void *tempBufferPixels, int windowWidth, int windowHeight);

#endif // GUARD_PLATFORM_SHARED_OPENGL_H
9 changes: 9 additions & 0 deletions src/background.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ NONMATCH("asm/non_matching/engine/sub_8002B20.inc", bool32 sub_8002B20(void))
s32 j;
u16 k;

#if (RENDERER == RENDERER_OPENGL)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might as well be consistent and do if (RENDERER == RENDERER_SOFTWARE) around the whole thing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For such a big function? I'd rather keep it with a short #ifdef.

// TEMP
return TRUE;
#endif

while (gBackgroundsCopyQueueCursor != gBackgroundsCopyQueueIndex) {
Background *bg;

Expand Down Expand Up @@ -626,6 +631,7 @@ END_NONMATCH

void UpdateBgAnimationTiles(Background *bg)
{
#if (RENDERER == RENDERER_SOFTWARE)
Tilemap *tilemap = gTilemapsRef[bg->tilemapId];
if (tilemap->animFrameCount > 0) {
if (tilemap->animDelay <= ++bg->animDelayCounter) {
Expand Down Expand Up @@ -660,6 +666,7 @@ void UpdateBgAnimationTiles(Background *bg)
}
}
}
#endif
}

// Differences to UpdateSpriteAnimation:
Expand Down Expand Up @@ -845,6 +852,7 @@ NONMATCH("asm/non_matching/engine/sub_80039E4.inc", bool32 sub_80039E4(void))
return TRUE;
#endif

#if (RENDERER == RENDERER_SOFTWARE)
if (gUnknown_03005390 != 0) {
OamDataShort oam;
s32 r5;
Expand Down Expand Up @@ -983,6 +991,7 @@ NONMATCH("asm/non_matching/engine/sub_80039E4.inc", bool32 sub_80039E4(void))

gUnknown_03005390 = 0;
}
#endif

return TRUE;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,9 @@ static bool32 ProcessVramGraphicsCopyQueue(void)
if ((graphics->src != 0) && (graphics->dest != 0))
#endif
{
#if (RENDERER == RENDERER_SOFTWARE)
DmaCopy16(3, (void *)(graphics->src + offset), (void *)(graphics->dest + offset), COPY_CHUNK_SIZE);
#endif
graphics->size -= COPY_CHUNK_SIZE;
}
#ifdef BUG_FIX
Expand All @@ -733,7 +735,9 @@ static bool32 ProcessVramGraphicsCopyQueue(void)
if ((graphics->src != 0) && (graphics->dest != 0))
#endif
{
#if (RENDERER == RENDERER_SOFTWARE)
DmaCopy16(3, (void *)(graphics->src + offset), (void *)(graphics->dest + offset), graphics->size);
#endif
}
graphics->size = 0;
}
Expand Down
4 changes: 4 additions & 0 deletions src/game/interactables_2/egg_utopia/cannon.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ NONMATCH("asm/non_matching/game/interactables_2/egg_utopia/sub_807E66C.inc", boo
s32 biggerX, biggerY, temp2, temp3;
s32 r4;
s16 playerX, playerY;
#ifndef NON_MATCHING
freshollie marked this conversation as resolved.
Show resolved Hide resolved
register Sprite_Cannon *r3 asm("r3") = cannon;
#else
Sprite_Cannon *r3 = cannon;
#endif
Sprite *s2 = &r3->sprite2;
if (!PLAYER_IS_ALIVE) {
return 0;
Expand Down
27 changes: 20 additions & 7 deletions src/game/stage/intro.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ struct Task *SetupStageIntro(void)
gStageFlags |= STAGE_FLAG__ACT_START;
gStageFlags |= STAGE_FLAG__100;

// These are null when super sonic
// NOTE: null when Super Sonic
#ifdef BUG_FIX
if (gPlayer.spriteInfoBody)
#endif
Expand Down Expand Up @@ -547,13 +547,26 @@ static void Task_IntroControllerMain(void)
p->anim = characterAnimsGettingReady[gSelectedCharacter].anim;
p->variant = characterAnimsGettingReady[gSelectedCharacter].variant;
p->unk6C = TRUE;
p->spriteInfoBody->s.frameFlags |= MOVESTATE_40000;
p->spriteInfoLimbs->s.frameFlags |= MOVESTATE_40000;

if (IS_MULTI_PLAYER) {
p->spriteInfoBody->s.palId = SIO_MULTI_CNT->id;
} else {
p->spriteInfoBody->s.palId = 0;
// NOTE: null when Super Sonic
#ifdef BUG_FIX
freshollie marked this conversation as resolved.
Show resolved Hide resolved
if (p->spriteInfoBody != NULL)
#endif
{
p->spriteInfoBody->s.frameFlags |= MOVESTATE_40000;

#ifdef BUG_FIX
if (p->spriteInfoLimbs != NULL)
#endif
{
p->spriteInfoLimbs->s.frameFlags |= MOVESTATE_40000;
}

if (IS_MULTI_PLAYER) {
p->spriteInfoBody->s.palId = SIO_MULTI_CNT->id;
} else {
p->spriteInfoBody->s.palId = 0;
}
}
}

Expand Down
55 changes: 1 addition & 54 deletions src/platform/pret_sdl/sdl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "gba/types.h"
#include "lib/agb_flash/flash_internal.h"
#include "platform/shared/dma.h"
#include "platform/shared/input.h"

#if ENABLE_AUDIO
#include "platform/shared/audio/cgb_audio.h"
Expand Down Expand Up @@ -494,60 +495,6 @@ void ProcessSDLEvents(void)
}
}

#ifdef _WIN32
#define STICK_THRESHOLD 0.5f
u16 GetXInputKeys()
{
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));

DWORD dwResult = XInputGetState(0, &state);
u16 xinputKeys = 0;

if (dwResult == ERROR_SUCCESS) {
/* A */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_A) >> 12;
/* B */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_X) >> 13;
/* Start */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_START) >> 1;
/* Select */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) >> 3;
/* L */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) << 1;
/* R */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) >> 1;
/* Up */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) << 6;
/* Down */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) << 6;
/* Left */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) << 3;
/* Right */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) << 1;

/* Control Stick */
float xAxis = (float)state.Gamepad.sThumbLX / (float)SHRT_MAX;
float yAxis = (float)state.Gamepad.sThumbLY / (float)SHRT_MAX;

if (xAxis < -STICK_THRESHOLD)
xinputKeys |= DPAD_LEFT;
else if (xAxis > STICK_THRESHOLD)
xinputKeys |= DPAD_RIGHT;
if (yAxis < -STICK_THRESHOLD)
xinputKeys |= DPAD_DOWN;
else if (yAxis > STICK_THRESHOLD)
xinputKeys |= DPAD_UP;

/* Speedup */
// Note: 'speedup' variable is only (un)set on keyboard input
double oldTimeScale = timeScale;
timeScale = (state.Gamepad.bRightTrigger > 0x80 || speedUp) ? 5.0 : 1.0;

if (oldTimeScale != timeScale) {
if (timeScale > 1.0) {
SDL_PauseAudio(1);
} else {
SDL_ClearQueuedAudio(1);
SDL_PauseAudio(0);
}
}
}

return xinputKeys;
}
#endif // _WIN32

u16 Platform_GetKeyInput(void)
{
#ifdef _WIN32
Expand Down
44 changes: 44 additions & 0 deletions src/platform/shared/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifdef _WIN32
#include <windows.h>
#include <xinput.h>
#include "gba/io_reg.h"
#include "gba/types.h"

#define STICK_THRESHOLD 0.5f
u16 GetXInputKeys()
{
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));

DWORD dwResult = XInputGetState(0, &state);
u16 xinputKeys = 0;

if (dwResult == ERROR_SUCCESS) {
/* A */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_A) >> 12;
/* B */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_X) >> 13;
/* Start */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_START) >> 1;
/* Select */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) >> 3;
/* L */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) << 1;
/* R */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) >> 1;
/* Up */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) << 6;
/* Down */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) << 6;
/* Left */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) << 3;
/* Right */ xinputKeys |= (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) << 1;

/* Control Stick */
float xAxis = (float)state.Gamepad.sThumbLX / (float)SHRT_MAX;
float yAxis = (float)state.Gamepad.sThumbLY / (float)SHRT_MAX;

if (xAxis < -STICK_THRESHOLD)
xinputKeys |= DPAD_LEFT;
else if (xAxis > STICK_THRESHOLD)
xinputKeys |= DPAD_RIGHT;
if (yAxis < -STICK_THRESHOLD)
xinputKeys |= DPAD_DOWN;
else if (yAxis > STICK_THRESHOLD)
xinputKeys |= DPAD_UP;
}

return xinputKeys;
}
#endif // _WIN32
Loading