Skip to content

Commit

Permalink
Persist window resolution and fullscreen preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
joao18araujo committed Jun 13, 2017
1 parent 37ef894 commit b2df3aa
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 15 deletions.
24 changes: 24 additions & 0 deletions include/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef CONFIG_H
#define CONFIG_H

#include <string>

#define RES_FOLDER string("res/")

using std::string;

class Config{
public:
static void init();
static int get_width();
static int get_height();
static int is_fullscreen();
static void update_information(int cwidth, int cheight, int cfullscreen);

private:
static int width;
static int height;
static int fullscreen;
};

#endif
2 changes: 1 addition & 1 deletion include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Game{
void update_resolution();

public:
Game(string title, int width, int height);
Game(string title);
~Game();

static Game & get_instance();
Expand Down
1 change: 1 addition & 0 deletions res/config_file.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1024 768 0
3 changes: 2 additions & 1 deletion src/BattleState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "MenuState.h"
#include "FighterStats.h"
#include "TimeCounter.h"
#include "Config.h"

#define N_BACKGROUND 2

Expand Down Expand Up @@ -95,7 +96,7 @@ void BattleState::resume(){
void BattleState::read_level_design(string stage){
float x, y, width, crotation;
int platform;
fstream level_design("res/stage_" + stage + "/level_design.dat");
fstream level_design(RES_FOLDER + "stage_" + stage + "/level_design.dat");
if(not level_design.is_open()){
printf("Level design of stage %s can't be opened\n", stage.c_str());
exit(-5);
Expand Down
34 changes: 34 additions & 0 deletions src/Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "Config.h"

#include <fstream>

int Config::width;
int Config::height;
int Config::fullscreen;

void Config::init(){
std::fstream config_file(RES_FOLDER + "config_file.dat");
config_file >> width >> height >> fullscreen;
}

int Config::get_width(){
return width;
}

int Config::get_height(){
return height;
}

int Config::is_fullscreen(){
return fullscreen;
}

void Config::update_information(int cwidth, int cheight, int cfullscreen){
width = cwidth;
height = cheight;
fullscreen = cfullscreen;
std::ofstream config_file(RES_FOLDER + "config_file.dat", std::ios::trunc);
config_file << width << " " << height << " " << fullscreen << std::endl;
config_file.close();

}
11 changes: 6 additions & 5 deletions src/EditState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Fighter.h"
#include "EditableFloor.h"
#include "MenuState.h"
#include "Config.h"

#define WIDTH 1280
#define HEIGHT 720
Expand Down Expand Up @@ -92,7 +93,7 @@ void EditState::resume(){
void EditState::read_level_design(){
float x, y, width, crotation;
int platform;
ifstream level_design("res/stage_" + stage + "/level_design.dat");
ifstream level_design(RES_FOLDER + "stage_" + stage + "/level_design.dat");
if(not level_design.is_open()){
printf("Level design of stage %s can't be opened\n", stage.c_str());
exit(-5);
Expand Down Expand Up @@ -129,14 +130,14 @@ void EditState::read_level_design(){


void EditState::update_level_design(){
ifstream level_design("res/stage_" + stage + "/level_design.dat", std::ios::binary);
ofstream old_level_design("res/stage_" + stage + "/level_design.dat.old", std::ios::trunc | std::ios::binary);
ifstream level_design(RES_FOLDER + "stage_" + stage + "/level_design.dat", std::ios::binary);
ofstream old_level_design(RES_FOLDER + "stage_" + stage + "/level_design.dat.old", std::ios::trunc | std::ios::binary);
old_level_design << level_design.rdbuf();
level_design.close();
old_level_design.close();

ofstream new_level_design("res/stage_" + stage + "/level_design.dat", std::ios::trunc);
ifstream backup("res/stage_" + stage + "/level_design.dat.old", std::ios::binary);
ofstream new_level_design(RES_FOLDER + "stage_" + stage + "/level_design.dat", std::ios::trunc);
ifstream backup(RES_FOLDER + "stage_" + stage + "/level_design.dat.old", std::ios::binary);
string s;
for(unsigned i = 0; i <= backgrounds.size(); ++i){
std::getline(backup, s);
Expand Down
14 changes: 11 additions & 3 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

#include "InputManager.h"
#include "Resources.h"
#include "Config.h"

#include <cstdlib>

Game * Game::instance = nullptr;

Game::Game(string title, int cwidth, int cheight){
Game::Game(string title){
instance = instance ? instance : this;
frame_start = SDL_GetTicks();
delta = 0;

srand(time(nullptr));

Config::init();
int cwidth = Config::get_width();
int cheight = Config::get_height();

int sdl_init = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
if(sdl_init){
printf("%s\n", SDL_GetError());
Expand All @@ -33,7 +38,7 @@ Game::Game(string title, int cwidth, int cheight){
exit(-1);
}

// SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
if(Config::is_fullscreen()) SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);

renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(renderer == nullptr){
Expand Down Expand Up @@ -66,7 +71,7 @@ Game::Game(string title, int cwidth, int cheight){
printf("Warning: No joysticks connected!\n");
}

SDL_GameControllerAddMappingsFromFile("res/joysticks/gamecontrollerdb.txt");
SDL_GameControllerAddMappingsFromFile((RES_FOLDER + "joysticks/gamecontrollerdb.txt").c_str());

stored_state = nullptr;
}
Expand Down Expand Up @@ -165,6 +170,7 @@ void Game::update_resolution() {
SDL_GetWindowSize(window, &width, &height);

printf("W: %d, H: %d\n", width, height);
Config::update_information(width, height, Config::is_fullscreen());

float original_ratio = 1280.0 / 720;
float new_ratio = (1.0 * width) / height;
Expand All @@ -189,5 +195,7 @@ void Game::change_resolution(int cwidth, int cheight){
}

void Game::set_fullscreen(bool on){
SDL_GetWindowSize(window, &width, &height);
Config::update_information(width, height, (int) on);
SDL_SetWindowFullscreen(window, on ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
}
2 changes: 1 addition & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "MenuState.h"

int main(int, char **){
Game game("Wenova - Rise of Conquerors", 1024, 768);
Game game("Wenova - Rise of Conquerors");

State * state = new MenuState(false);
game.push(state);
Expand Down
3 changes: 2 additions & 1 deletion src/Music.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Music.h"

#include "Resources.h"
#include "Config.h"

Music::Music(){
music = nullptr;
Expand All @@ -27,7 +28,7 @@ void Music::stop(){
}

void Music::open(string file){
music = Resources::get_music("res/" + file);
music = Resources::get_music(RES_FOLDER + file);
}

bool Music::is_open(){
Expand Down
3 changes: 2 additions & 1 deletion src/Sound.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Sound.h"

#include "Resources.h"
#include "Config.h"

Sound::Sound(){
sound = nullptr;
Expand All @@ -23,7 +24,7 @@ void Sound::stop(){
}

void Sound::open(string file){
sound = Resources::get_sound("res/" + file);
sound = Resources::get_sound(RES_FOLDER + file);
}

bool Sound::is_open(){
Expand Down
3 changes: 2 additions & 1 deletion src/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Sprite.h"
#include "Game.h"
#include "Resources.h"
#include "Config.h"

#include <cmath>

Expand All @@ -24,7 +25,7 @@ Sprite::Sprite(string file, int cframe_count, float cframe_time, int ccolumns, i
rows = ceil(frame_count / columns);
time_elapsed = 0;
texture = nullptr;
open("res/" + file);
open(RES_FOLDER + file);

scale_x = scale_y = 1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Game.h"
#include "Resources.h"
#include "Config.h"

#define SOLID Text::TextStyle::SOLID
#define SHADED Text::TextStyle::SHADED
Expand Down Expand Up @@ -135,5 +136,5 @@ void Text::remake_texture(){
}

void Text::open(string file, int size){
font = Resources::get_font("res/" + file, size);
font = Resources::get_font(RES_FOLDER + file, size);
}

0 comments on commit b2df3aa

Please sign in to comment.