Skip to content

Commit

Permalink
add sfml library support (todo: add shader support)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cpasjuste committed Jan 23, 2017
1 parent 90c12ac commit 2b4824b
Show file tree
Hide file tree
Showing 18 changed files with 712 additions and 114 deletions.
58 changes: 30 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ set(CMAKE_SYSTEM_NAME "Generic")
enable_language(ASM)

# delete cmake cache folder before changing this options
option(BUILD_SDL2 "Build with SDL2 support" ON)
option(BUILD_SFML "Build with SFML support" OFF)
option(BUILD_PSP2 "Build with PSP2 support" OFF)
option(BUILD_RPI "Build with RPI support" OFF)

Expand Down Expand Up @@ -299,7 +301,7 @@ if (BUILD_DEBUG)
list(APPEND FLAGS -O0 -g -DDEBUG)
else ()
list(APPEND FLAGS -O3 -DNDEBUG)
endif(BUILD_DEBUG)
endif (BUILD_DEBUG)

#################
# PSP2 (ps vita)
Expand All @@ -325,23 +327,14 @@ if (BUILD_PSP2)
list(APPEND FLAGS
-Wl,-q -D__PSP2__
-DBUILD_C68K -DUSE_FILE32API
-marm -mfpu=neon -mcpu=cortex-a9 -march=armv7-a -mfloat-abi=hard -ffast-math
-fno-asynchronous-unwind-tables -funroll-loops
-mword-relocations -fno-unwind-tables -fno-optimize-sibling-calls
-mvectorize-with-neon-quad -funsafe-math-optimizations
-mlittle-endian -munaligned-access
)
if (BUILD_DEBUG)
list(APPEND FLAGS -D__PSP2_DEBUG__
-marm -mfpu=neon -mcpu=cortex-a9 -march=armv7-a -mfloat-abi=hard -ffast-math
-fno-asynchronous-unwind-tables -funroll-loops
-mword-relocations -fno-unwind-tables -fno-optimize-sibling-calls
-mvectorize-with-neon-quad -funsafe-math-optimizations
-mlittle-endian -munaligned-access
-O3)
else ()
list(APPEND FLAGS
-marm -mfpu=neon -mcpu=cortex-a9 -march=armv7-a -mfloat-abi=hard -ffast-math
-fno-asynchronous-unwind-tables -funroll-loops
-mword-relocations -fno-unwind-tables -fno-optimize-sibling-calls
-mvectorize-with-neon-quad -funsafe-math-optimizations
-mlittle-endian -munaligned-access
)
list(APPEND FLAGS -D__PSP2_DEBUG__ -O3) # cyclone doesn't work without optimisations
endif (BUILD_DEBUG)
set(LDFLAGS
SDL2
Expand All @@ -365,15 +358,24 @@ else ()
##############
# SDL2
##############
list(APPEND INC
pfba/sdl2
)
file(GLOB SRC_SDL2
pfba/sdl2/*.c*
)
list(APPEND SRC_PFBA ${SRC_SDL2})
list(APPEND FLAGS )
set(LDFLAGS SDL2 SDL2_image SDL2_ttf png z)
if (BUILD_SDL2)
list(APPEND FLAGS -D__SDL2__)
list(APPEND INC pfba/sdl2)
file(GLOB SRC_SDL2 pfba/sdl2/*.c*)
list(APPEND SRC_PFBA ${SRC_SDL2})
set(LDFLAGS SDL2 SDL2_image SDL2_ttf png z)
##############
# SFML
##############
elseif (BUILD_SFML)
list(APPEND FLAGS -D__SFML__)
list(APPEND INC pfba/sfml)
file(GLOB SRC_SFML
pfba/sdl2/sdl2_audio.cpp # TODO: get rid of sdl2, use sdl2 audio for now
pfba/sfml/*.c*)
list(APPEND SRC_PFBA ${SRC_SFML})
list(APPEND LDFLAGS SDL2 GL sfml-graphics sfml-window sfml-system png z)
endif (BUILD_SDL2)
##############
# RPI
##############
Expand Down Expand Up @@ -416,9 +418,9 @@ endif (BUILD_PSP2)
# source files
set(SRC ${SRC_PFBA} ${SRC_CPU} ${SRC_DRV} ${SRC_BURN} ${SRC_BURNER} ${SRC_INTF} ${SRC_7Z} ${SRC_LIBCONFIG})

#####################
# SDL2 (unix) target
#####################
##########################
# SDL2/SFML (unix) target
##########################
add_executable(${PROJECT_NAME} ${SRC} ${CMAKE_BINARY_DIR}/deps/m68kops.c)
target_compile_options(${PROJECT_NAME} PRIVATE ${FLAGS})
target_include_directories(${PROJECT_NAME} PRIVATE ${INC})
Expand Down
31 changes: 13 additions & 18 deletions pfba/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@
*/

#include <gui/gui.h>
#include <sdl2/sdl2_input.h>

#ifdef __PSP2__
#include <psp2/power.h>
#include <psp2/io/dirent.h>
#include <psp2/psp2_utility.h>
int _newlib_heap_size_user = 192 * 1024 * 1024;
#elif __RPI__
#include <sdl2/sdl2_utility.h>
#else

#include <sdl2/sdl2_utility.h>

#elif __SDL2__
#include <sdl2/sdl2_input.h>
#elif __SFML__
#include <sfml/sfml_input.h>
#endif

Renderer *renderer;
Expand Down Expand Up @@ -93,12 +90,12 @@ int main(int argc, char **argv) {

renderer = (Renderer *) new PSP2Renderer(960, 544);
utility = (Utility*)new PSP2Utility();
#elif __RPI__
renderer = (Renderer *) new SDL2Renderer(0, 0);
utility = (Utility*)new SDL2Utility();
#else
#elif __SDL2__
renderer = (Renderer *) new SDL2Renderer(960, 544);
utility = (Utility *) new SDL2Utility();
utility = new Utility();
#elif __SFML__
renderer = (Renderer *) new SFMLRenderer(960, 544);
utility = new Utility();
#endif

BurnPathsInit();
Expand All @@ -109,15 +106,13 @@ int main(int argc, char **argv) {
cfgPath += "/pfba.cfg";
config = new Config(cfgPath);

#ifdef __PSP2__
// init input
inp = (Input *) new SDL2Input();
#elif __RPI__
// init input
#ifdef __PSP2__
inp = (Input *) new SDL2Input();
#else
// init input
#elif __SDL2__
inp = (Input *) new SDL2Input();
#elif __SFML__
inp = (Input *) new SFMLInput((SFMLRenderer*)renderer);
#endif

// build/init roms list
Expand Down
6 changes: 0 additions & 6 deletions pfba/psp2/psp2_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ class PSP2Renderer : Renderer {
advanced_aa,
scale2x,
end
//fxaa
//crt_easymode,
//gtu,
//xbr2x,
//xbr2x_fast,
//bicubic,
};

private:
Expand Down
49 changes: 25 additions & 24 deletions pfba/sdl2/sdl2_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "sdl2_input.h"

static int key_id[Input::Key::KEY_COUNT] {
static int key_id[Input::Key::KEY_COUNT]{
Input::Key::KEY_UP,
Input::Key::KEY_DOWN,
Input::Key::KEY_LEFT,
Expand All @@ -22,7 +22,7 @@ static int key_id[Input::Key::KEY_COUNT] {
SDL2Input::SDL2Input() {

if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) {
printf("SDL2Input: SDL_InitSubSystem\n");
printf("SDL2Input: SDL_INIT_JOYSTICK\n");
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
}

Expand All @@ -36,24 +36,25 @@ SDL2Input::SDL2Input() {
for (int i = 0; i < joystick_count; i++) {
printf("Joystick: %i\n", i);
players[i].data = SDL_JoystickOpen(i);
players[i].id = i;
players[i].enabled = true;
printf("Name: %s\n", SDL_JoystickName((SDL_Joystick *)players[i].data));
printf("Hats %d\n", SDL_JoystickNumHats((SDL_Joystick *)players[i].data));
printf("Buttons %d\n", SDL_JoystickNumButtons((SDL_Joystick *)players[i].data));
printf("Axis %d\n", SDL_JoystickNumAxes((SDL_Joystick *)players[i].data));
printf("Name: %s\n", SDL_JoystickName((SDL_Joystick *) players[i].data));
printf("Hats %d\n", SDL_JoystickNumHats((SDL_Joystick *) players[i].data));
printf("Buttons %d\n", SDL_JoystickNumButtons((SDL_Joystick *) players[i].data));
printf("Axis %d\n", SDL_JoystickNumAxes((SDL_Joystick *) players[i].data));
}
} else {
// allow keyboard mapping to player1
players[0].enabled = true;
}

for(int i=0; i<PLAYER_COUNT; i++) {
for(int k=0; k<KEY_COUNT; k++) {
for (int i = 0; i < PLAYER_COUNT; i++) {
for (int k = 0; k < KEY_COUNT; k++) {
players[i].mapping[k] = 0;
}
}

for(int i=0; i<KEY_COUNT; i++) {
for (int i = 0; i < KEY_COUNT; i++) {
keyboard.mapping[i] = 0;
}
}
Expand Down Expand Up @@ -85,17 +86,17 @@ Input::Player *SDL2Input::Update(bool rotate) {
players[i].state = 0;
}


SDL_Event event;
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) {
players[0].state |= KEY_QUIT;
return players;
while(SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
players[0].state |= KEY_QUIT;
return players;
}
}

for(int i=0; i<PLAYER_COUNT; i++) {
for (int i = 0; i < PLAYER_COUNT; i++) {

if(!players[i].enabled) {
if (!players[i].enabled) {
continue;
}

Expand All @@ -115,9 +116,9 @@ Input::Player *SDL2Input::Update(bool rotate) {
return players;
}

void SDL2Input::process_axis(Input::Player& player, bool rotate) {
void SDL2Input::process_axis(Input::Player &player, bool rotate) {

if(!player.enabled || !player.data) {
if (!player.enabled || !player.data) {
return;
}

Expand All @@ -140,9 +141,9 @@ void SDL2Input::process_axis(Input::Player& player, bool rotate) {
}
}

void SDL2Input::process_hat(Input::Player& player, bool rotate) {
void SDL2Input::process_hat(Input::Player &player, bool rotate) {

if(!player.enabled || !player.data) {
if (!player.enabled || !player.data) {
return;
}

Expand Down Expand Up @@ -172,11 +173,11 @@ void SDL2Input::process_hat(Input::Player& player, bool rotate) {

void SDL2Input::process_buttons(Input::Player &player, bool rotate) {

if(!player.enabled || !player.data) {
if (!player.enabled || !player.data) {
return;
}

for(int i=0; i<KEY_COUNT; i++) {
for (int i = 0; i < KEY_COUNT; i++) {

int mapping = player.mapping[i];

Expand Down Expand Up @@ -218,11 +219,11 @@ void SDL2Input::process_buttons(Input::Player &player, bool rotate) {
}
}

void SDL2Input::process_keyboard(Input::Player& player, bool rotate) {
void SDL2Input::process_keyboard(Input::Player &player, bool rotate) {

const Uint8 *keys = SDL_GetKeyboardState(NULL);

for(int i=0; i<KEY_COUNT; i++) {
for (int i = 0; i < KEY_COUNT; i++) {
if (keys[keyboard.mapping[i]]) {
if (rotate && key_id[i] == Input::Key::KEY_UP) {
player.state |= Input::Key::KEY_RIGHT;
Expand Down
19 changes: 0 additions & 19 deletions pfba/sdl2/sdl2_utility.h

This file was deleted.

50 changes: 50 additions & 0 deletions pfba/sfml/sfml_font.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Created by cpasjuste on 01/12/16.
//

#include "sfml_font.h"

SFMLFont::SFMLFont(const char *path, int size) : Font(path, size) {
this->size = size;

if (!font.loadFromFile(path)) {
printf("SFMLFont: load font error\n");
}
}

SFMLFont::~SFMLFont() {
}

int SFMLFont::GetWidth(const char *fmt, ...) {

char msg[MAX_PATH];
memset(msg, 0, MAX_PATH);
va_list args;
va_start(args, fmt);
vsnprintf(msg, MAX_PATH, fmt, args);
va_end(args);

sf::Text t;
t.setFont(font);
t.setString(msg);
t.setCharacterSize(size);

return (int)t.getLocalBounds().width;
}

int SFMLFont::GetHeight(const char *fmt, ...) {

char msg[MAX_PATH];
memset(msg, 0, MAX_PATH);
va_list args;
va_start(args, fmt);
vsnprintf(msg, MAX_PATH, fmt, args);
va_end(args);

sf::Text t;
t.setFont(font);
t.setString(msg);
t.setCharacterSize(size);

return (int)t.getLocalBounds().height;
}
26 changes: 26 additions & 0 deletions pfba/sfml/sfml_font.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by cpasjuste on 01/12/16.
//

#ifndef SFML_FONT_H_
#define SFML_FONT_H_

#include <skeleton/renderer.h>
#include <skeleton/font.h>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Text.hpp>

class SFMLFont : Font {

public:
SFMLFont(const char *path, int size);

~SFMLFont();

virtual int GetWidth(const char *fmt, ...);
virtual int GetHeight(const char *fmt, ...);

sf::Font font;
};

#endif //_SDL2_FONT_H_
Loading

0 comments on commit 2b4824b

Please sign in to comment.