Skip to content

Commit

Permalink
Add support for GP2X
Browse files Browse the repository at this point in the history
  • Loading branch information
M-HT committed Jan 17, 2016
1 parent 3e5581a commit f19fbba
Show file tree
Hide file tree
Showing 5 changed files with 528 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/Makefile-gp2x
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
all: alien

CC=arm-open2x-linux-gcc
SDL_CFLAGS = `sdl-config --cflags`
SDL_LIBS = `sdl-config --libs`
CFLAGS=-DNDEBUG -Wall $(SDL_CFLAGS) -DGP2X -O2 -pipe -march=armv4t -mtune=arm920t -ftree-vectorize -ffast-math -fsingle-precision-constant
OBJS=\
client.o common.o vm.o sprites.o decode.o animation.o \
rooms.o render.o main.o music.o debug.o lzss.o \
sound.o screen.o scale2x.o scale3x.o game2bin.o cd_iso.o \
scale800x480.o

LIBS=-s -L`sdl-config --prefix`/lib/mixer-ogg -lSDL_mixer $(SDL_LIBS) -lvorbisidec -lvorbisfile -lm -larmmem -static

alien: $(OBJS)
$(CC) -o $@ $(OBJS) $(LIBS)

.c.o:
$(CC) $(CFLAGS) -c $< -o $@

clean:
@rm $(OBJS)
238 changes: 237 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
#include <assert.h>
#include <SDL.h>
#include <SDL_mixer.h>
#if defined(GP2X)
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/soundcard.h>
#include <fcntl.h>
#if (SDL_MAJOR_VERSION > 1 || SDL_MAJOR_VERSION == 1 && (SDL_MINOR_VERSION > 2 || SDL_MINOR_VERSION == 2 && SDL_PATCHLEVEL >= 9 ) )
#include <SDL_gp2x.h>
#endif
#endif

#include "client.h"
#include "vm.h"
Expand Down Expand Up @@ -137,14 +146,116 @@ static void atexit_callback(void)
SDL_Quit();
}

#if defined(GP2X)
static int InitialVolume;

// Set new GP2X mixer level, 0-100
static void Set_GP2X_Volume (int newvol)
{
int soundDev, vol;

if ((newvol >= 0) && (newvol <= 100))
{
soundDev = open("/dev/mixer", O_RDWR);
if (soundDev != -1)
{
vol = ((newvol << 8) | newvol);
ioctl(soundDev, SOUND_MIXER_WRITE_PCM, &vol);
close(soundDev);
}
}
}

// Returns 0-100, current mixer volume, -1 on error.
static int Get_GP2X_Volume (void)
{
int soundDev, vol;

vol = -1;
soundDev = open("/dev/mixer", O_RDONLY);
if (soundDev != -1)
{
ioctl(soundDev, SOUND_MIXER_READ_PCM, &vol);
close(soundDev);
if (vol != -1)
{
//just return one channel , not both channels, they're hopefully the same anyways
return (vol & 0xFF);
}
}

return vol;
}

static void Set_Initial_GP2X_Volume (void)
{
Set_GP2X_Volume(InitialVolume);
}

static void Change_HW_Audio_Volume (int amount)
{
int current_volume;

current_volume = Get_GP2X_Volume();

if (current_volume == -1) current_volume = 68;

if ((amount > 1) && current_volume < 12)
{
amount = 1;
}
else if ((amount < -1) && current_volume <= 12)
{
amount = -1;
}

current_volume += amount;

if (current_volume > 100)
{
current_volume = 100;
}
else if (current_volume < 0)
{
current_volume = 0;
}
Set_GP2X_Volume(current_volume);
}
#endif

static int initialize()
{
#if defined(GP2X)
InitialVolume = Get_GP2X_Volume();
atexit(Set_Initial_GP2X_Volume);
#endif

SDL_Init(SDL_INIT_VIDEO|SDL_INIT_CDROM|SDL_INIT_AUDIO);
atexit(atexit_callback);

#if defined(GP2X)
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK)<0)
{
panic("failed to initialize SDL joystick subsystem");
}
SDL_JoystickOpen(0);
#if defined(SDL_GP2X__H)
if (SDL_GP2X_MouseType() == GP2X_MOUSE_TOUCHSCREEN)
{
SDL_GP2X_TouchpadMouseMotionEvents(0);
SDL_GP2X_TouchpadMouseButtonEvents(0);
}
#endif
#endif

if (cls.nosound == 0)
{
if (Mix_OpenAudio(44100, AUDIO_S16, 2, 4096) < 0)
#if defined(GP2X)
#define MIX_BUFFER_SIZE 1024
#else
#define MIX_BUFFER_SIZE 4096
#endif
if (Mix_OpenAudio(44100, AUDIO_S16, 2, MIX_BUFFER_SIZE) < 0)
{
panic("Mix_OpenAudio failed\n");
}
Expand Down Expand Up @@ -173,6 +284,10 @@ static int initialize()
panic("failed to create video surface");
}

#if defined(GP2X)
Set_Initial_GP2X_Volume();
#endif

return 0;
}

Expand Down Expand Up @@ -642,6 +757,127 @@ void check_events()
}
break;

#if defined(GP2X)
// GP2X buttons
#define GP2X_BUTTON_UP (0)
#define GP2X_BUTTON_DOWN (4)
#define GP2X_BUTTON_LEFT (2)
#define GP2X_BUTTON_RIGHT (6)
#define GP2X_BUTTON_UPLEFT (1)
#define GP2X_BUTTON_UPRIGHT (7)
#define GP2X_BUTTON_DOWNLEFT (3)
#define GP2X_BUTTON_DOWNRIGHT (5)
#define GP2X_BUTTON_CLICK (18)
#define GP2X_BUTTON_A (12)
#define GP2X_BUTTON_B (13)
#define GP2X_BUTTON_X (14)
#define GP2X_BUTTON_Y (15)
#define GP2X_BUTTON_L (10)
#define GP2X_BUTTON_R (11)
#define GP2X_BUTTON_START (8)
#define GP2X_BUTTON_SELECT (9)
#define GP2X_BUTTON_VOLUP (16)
#define GP2X_BUTTON_VOLDOWN (17)

case SDL_JOYBUTTONUP:
switch(event.jbutton.button)
{
case GP2X_BUTTON_RIGHT:
key_right = 0;
break;

case GP2X_BUTTON_LEFT:
key_left = 0;
break;

case GP2X_BUTTON_UP:
key_up = 0;
break;

case GP2X_BUTTON_DOWN:
key_down = 0;
break;

case GP2X_BUTTON_X:
key_a = 0;
break;

case GP2X_BUTTON_B:
key_b = 0;
break;

case GP2X_BUTTON_A:
key_c = 0;
break;

default:
/* keep -Wall happy */
break;
}
break;

case SDL_JOYBUTTONDOWN:
switch(event.jbutton.button)
{
case GP2X_BUTTON_SELECT:
cls.quit = 1;
break;

case GP2X_BUTTON_RIGHT:
key_right = 1;
break;

case GP2X_BUTTON_LEFT:
key_left = 1;
break;

case GP2X_BUTTON_UP:
key_up = 1;
break;

case GP2X_BUTTON_DOWN:
key_down = 1;
break;

case GP2X_BUTTON_X:
key_a = 1;
break;

case GP2X_BUTTON_B:
key_b = 1;
break;

case GP2X_BUTTON_A:
key_c = 1;
break;

case GP2X_BUTTON_L:
quicksave();
break;

case GP2X_BUTTON_R:
quickload();
break;

case GP2X_BUTTON_START:
toggle_scaling();
break;

case GP2X_BUTTON_VOLUP:
Change_HW_Audio_Volume(4);
break;

case GP2X_BUTTON_VOLDOWN:
Change_HW_Audio_Volume(-4);
break;

default:
/* keep -Wall happy */
break;
}
break;
#endif

case SDL_QUIT:
leave_game();
break;
Expand Down
2 changes: 2 additions & 0 deletions src/music.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ static void play_music_track_mp3(int track, int loop)
{
sprintf(filename, ISO_PREFIX "%02d.ogg", track + 1);
}
#if !defined(GP2X)
// if ogg file doesn't exist use mp3
if (access(filename, R_OK))
{
Expand All @@ -103,6 +104,7 @@ static void play_music_track_mp3(int track, int loop)
sprintf(filename, ISO_PREFIX "%02d.mp3", track + 1);
}
}
#endif
LOG(("playing mp3 %s\n", filename));

current_track = Mix_LoadMUS(filename);
Expand Down
Loading

0 comments on commit f19fbba

Please sign in to comment.