Skip to content

Commit d888822

Browse files
author
Thomas Perl
committed
Port to Sailfish OS
1 parent 52cf7de commit d888822

10 files changed

+223
-101
lines changed

BPGame.cpp

+19-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "BPGame.h"
2020
#include "SDL.h"
2121

22+
#include "location.h"
23+
2224
#include <GLES/gl.h>
2325

2426
#include "SDL_ttf.h"
@@ -68,7 +70,14 @@ bool BPGame::ShowingClearScores;
6870
SpriteFont* BPGame::sfcMessageBoxText;
6971
SpriteFont* BPGame::sfcMessageBoxTitle;
7072

73+
// Implemented in main.cpp
74+
void
75+
bp_game_audio_set_last_music(const char *music);
76+
7177
void BPGame::Init(int width, int height) {
78+
this->width = width;
79+
this->height = height;
80+
7281
srand(time(0));
7382

7483
EnableSound = true;
@@ -1200,7 +1209,7 @@ void BPGame::AllocString(SpriteFont** tex, const char* str, FontSizes size, floa
12001209
SAFE_DELETE((*tex));
12011210

12021211
// it's incredibly lazy to open and close the font each time - hurray!
1203-
TTF_Font* fnt = TTF_OpenFont("/opt/brainparty/Content/freesans.ttf", size - 3); // NB: the -3 is here because the freesans.ttf font we're using is a big chunkier than the iPhone font
1212+
TTF_Font* fnt = TTF_OpenFont(DATA_DIR "freesans.ttf", size - 3); // NB: the -3 is here because the freesans.ttf font we're using is a big chunkier than the iPhone font
12041213

12051214
static SDL_Color white = { 255, 255, 255, 255 };
12061215

@@ -1305,7 +1314,7 @@ void BPGame::LoadSettings() {
13051314
NumUnlockedGames = 0;
13061315

13071316
ifstream ifs;
1308-
ifs.open("/home/user/.brainparty");
1317+
ifs.open(bp_get_save_file_name());
13091318

13101319
FirstRun = false;
13111320

@@ -1435,8 +1444,8 @@ void BPGame::LoadSettings() {
14351444
}
14361445

14371446
void BPGame::SaveSettings() {
1438-
ofstream savefile;
1439-
savefile.open("/home/user/.brainparty");
1447+
ofstream savefile;
1448+
savefile.open(bp_get_save_file_name());
14401449
savefile << EnableSound << endl;
14411450
savefile << EnableMusic << endl;
14421451
savefile << endl;
@@ -2347,9 +2356,11 @@ void BPGame::PlayMusic(const char* name) {
23472356
Mix_FreeMusic(Music);
23482357
Music = NULL;
23492358
}
2359+
2360+
bp_game_audio_set_last_music(name);
23502361

23512362
string* file = new string(name);
2352-
file->insert(0, "/opt/brainparty/Content/");
2363+
file->insert(0, DATA_DIR);
23532364
file->append(".ogg");
23542365

23552366
Music = Mix_LoadMUS(file->c_str());
@@ -2359,6 +2370,8 @@ void BPGame::PlayMusic(const char* name) {
23592370
}
23602371

23612372
void BPGame::StopMusic() {
2373+
bp_game_audio_set_last_music(NULL);
2374+
23622375
if (Music != NULL) {
23632376
Mix_HaltMusic();
23642377
Mix_FreeMusic(Music);
@@ -2376,7 +2389,7 @@ bool BPGame::FileExists(const char * filename) {
23762389

23772390
Mix_Chunk* BPGame::LoadSound(const char* filename, const char* extension) {
23782391
string* file = new string(filename);
2379-
file->insert(0, "/opt/brainparty/Content/");
2392+
file->insert(0, DATA_DIR);
23802393
file->append(".wav");
23812394

23822395
Mix_Chunk* retval = Mix_LoadWAV(file->c_str());

BPGame.h

+19-12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ struct cmp_str {
6161
};
6262

6363
class BPGame {
64+
private:
65+
int width;
66+
int height;
6467
public:
6568
int GLViewRenderbuffer;
6669
int GLViewFramebuffer;
@@ -206,29 +209,33 @@ class BPGame {
206209
inline void ConvertCoordinates(float &x, float &y) {
207210
/**
208211
* This converts the physical (screen) coordinates to the
209-
* game world coordinates. We've rotated the screen by 90
210-
* degrees, and the resolution is also different. We then
211-
* center the scaled image on the screen (the "-26"). The
212+
* game world coordinates.
213+
* We center the scaled image on the screen (the "-26"). The
212214
* "1.666/1.6" is here, because the screen has a ratio of
213-
* 480/800 and the world 480/320.
215+
* 800/480 (or a similar ratio) and the world is 480/320.
214216
*
215217
* world: screen:
216-
* a--b b--d
218+
* a--b a--b
217219
* | | | |
218-
* c--d a--c
219-
* 320x480 800x480
220+
* c--d c--d
221+
* 320x480 width x height
220222
*
221223
* The world is centered on the screen, so the world takes
222-
* up 720 pixels height on the screen - 40 pixels are black
224+
* up ... pixels height on the screen - .. pixels are black
223225
* border at the top (screen: left) and bottom (screen: right).
224226
*
225-
* These 40 pixels are ~ 26 pixels in world coordinates:
227+
* These .. pixels are ~ 26 pixels in world coordinates:
226228
* offset_in_world = (40 * 480 / 800) * 1.666 / 1.5
229+
*
230+
* TODO: Rewrite all this into a separate class that deals with
231+
* input projection as well as GL viewport/projection setup.
227232
**/
228-
float tmp = (480.-y)*320./480.;
229-
y = ((x*480./800.)*1.666/1.5)-26;
230-
x = tmp;
233+
float xx = x / width * 320.;
234+
float yy = ((y / height * 480.)*1.666/1.5)-26;
235+
x = xx;
236+
y = yy;
231237
}
238+
232239
void TouchStart(float x, float y);
233240
void TouchStop(float x, float y);
234241
void TouchDrag(float x, float y);

Makefile

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
1-
MACHINE= $(shell uname -s)
2-
OBJFILES := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
1+
TARGET := brainparty
32

4-
ifeq ($(MACHINE),Darwin)
5-
INCLUDES = -I/Library/Frameworks/SDL.framework/Headers -I/Library/Frameworks/SDL_image.framework/Headers -I/Library/Frameworks/SDL_mixer.framework/Headers -I/Library/Frameworks/SDL_ttf.framework/Headers -I/System/Library/Frameworks/OpenGL.framework/Headers
6-
LIBS = -m32 -framework SDL -framework SDL_image -framework SDL_mixer -framework SDL_ttf -framework Cocoa -framework OpenGL
3+
OBJFILES := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
74

8-
# SDL isn't in a great state on 64-bit Macs, so force 32-bit for now
9-
CXXFLAGS = -g -c -m32 -Wno-deprecated
5+
PKGS := sdl2 SDL2_mixer SDL2_ttf SDL2_image audioresource egl glesv1_cm glib-2.0
106

11-
# to compile on OS X you need to include this Objective C file
12-
OSXCOMPAT = SDLMain.m
13-
else
14-
INCLUDES = `sdl-config --cflags` -I/usr/X11R6/include
15-
LIBS = `sdl-config --libs`
16-
LIBS += -lEGL -lGLES_CM -lSDL_mixer -lSDL_ttf -lSDL_image
17-
CXXFLAGS = -O2 -c -Wno-deprecated
18-
OSXCOMPAT =
19-
endif
7+
CXXFLAGS += $(shell pkg-config --cflags $(PKGS))
8+
LIBS += $(shell pkg-config --libs $(PKGS))
209

21-
# object files have corresponding source files
22-
CXX = g++
10+
CXXFLAGS += -O2 -c -Wno-deprecated
2311

24-
all: brainparty
12+
all: $(TARGET)
2513

26-
brainparty: $(OBJFILES)
27-
$(CXX) -o brainparty $(INCLUDES) $(OSXCOMPAT) $(OBJFILES) $(LIBS)
14+
$(TARGET): $(OBJFILES)
15+
$(CXX) -o $@ $(OBJFILES) $(LIBS)
2816

2917
%.o: %.cpp
30-
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ $<
18+
$(CXX) $(CXXFLAGS) -o $@ $<
3119

3220
clean:
33-
rm -f brainparty *.o
21+
rm -f $(TARGET) $(OBJFILES)

Texture.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "Texture.h"
1919
#include "SDL_image.h"
20+
#include "location.h"
2021

2122
Texture::Texture(SDL_Surface* surface) {
2223
int po2width = Texture::NextPO2(surface->w);
@@ -37,7 +38,6 @@ Texture::Texture(SDL_Surface* surface) {
3738
#endif
3839
);
3940

40-
SDL_SetAlpha(surface, 0, 0);
4141
SDL_BlitSurface(surface, NULL, tmpsurface, NULL);
4242
InitWithSurface(tmpsurface, surface->w, surface->h);
4343
SDL_FreeSurface(tmpsurface);
@@ -48,7 +48,7 @@ Texture::Texture(SDL_Surface* surface) {
4848

4949
Texture::Texture(const char* filename, float actualwidth, float actualheight) {
5050
string file = filename;
51-
file.insert(0, "/opt/brainparty/Content/");
51+
file.insert(0, DATA_DIR);
5252
file.append(".png");
5353

5454
SDL_Surface *surface = IMG_Load(file.c_str());

brainparty.desktop

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
[Desktop Entry]
2-
Name=Brain Party
3-
Exec=invoker --single-instance --type=e /opt/brainparty/brainparty
4-
Icon=/opt/brainparty/brainparty.png
5-
Terminal=false
62
Type=Application
7-
Categories=Game;LogicGame;
3+
Name=Brain Party
4+
Exec=harbour-brainparty
5+
Icon=harbour-brainparty

location.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "location.h"
2+
3+
#include <glib.h>
4+
#include <stdio.h>
5+
6+
extern gchar *appname;
7+
8+
const char *bp_get_save_file_name()
9+
{
10+
static const char *save_file_name = NULL;
11+
12+
// Create save directory
13+
gchar *dirname = g_build_filename(g_get_user_data_dir(), appname, NULL);
14+
g_mkdir_with_parents(dirname, 0755);
15+
g_free(dirname);
16+
17+
if (!save_file_name) {
18+
save_file_name = g_build_filename(g_get_user_data_dir(), appname, "save", NULL);
19+
}
20+
21+
return save_file_name;
22+
}

location.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef BRAINPARTY_LOCATION_H
2+
#define BRAINPARTY_LOCATION_H
3+
4+
#define DATA_DIR "/usr/share/harbour-brainparty/"
5+
6+
const char *bp_get_save_file_name();
7+
8+
#endif /* BRAINPARTY_LOCATION_H */

0 commit comments

Comments
 (0)