I decided to make it an "Asteroids" way: write some general functions and divide them to smaller ones until you're done. If I didn't implement one function I put a printf in them to know it has been called.
Here's the code:
main.cpp
Code: Select all
#include "app.h"
extern "C"
int main(int, char*[]){
App app;
app.init();
app.run(app_splash);
while(app.running){
app.run(app_title);
app.run(app_game);
}
app.run(app_exit);
app.end();
return 0;
}
Code: Select all
#include "app.h"
#include <cstdlib>
#include <SDL/SDL.h>
#include "appspec.h"
void App::init(){
#ifdef DEBUG
freopen("log.txt", "w", stdout);
setbuf(stdout, NULL);
printf("App::init\n");
#endif
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK);
atexit(SDL_Quit);
screen = SDL_SetVideoMode(480, 272, 32, SDL_SWSURFACE);
SDL_JoystickEventState(SDL_ENABLE);
SDL_JoystickOpen(0);
running = true;
}
void App::run(app_state s){
#ifdef DEBUG
printf("App::run(");
#endif
switch(s){
case app_splash: stateSplash(); break;
case app_title: stateTitle(); break;
case app_game: stateGame(); break;
case app_exit: stateExit(); break;
default: error(AT);
}
}
void App::stateSplash(){
#ifdef DEBUG
printf("splash)\n");
#endif
}
void App::stateTitle(){
#ifdef DEBUG
printf("title)\n");
#endif
}
void App::stateExit(){
#ifdef DEBUG
printf("exit)\n");
#endif
}
void App::end(){
#ifdef DEBUG
printf("App::end\n");
#endif
}
Code: Select all
#ifndef GAME_H
#define GAME_H
#include "app.h"
struct Game {
bool running;
//----------//
void init();
void initPlayer();
void editor();
void run();
};
#endif // GAME_H
Code: Select all
#include "game.h"
#include "app.h"
void App::stateGame(){
#ifdef DEBUG
printf("game)\n");
#endif
Game game;
game.init(); // TODO: difficulty
game.initPlayer();
game.editor();
while(true){
game.run();
if(!game.running) break;
game.editor();
if(!game.running) break;
}
running = false;
}
void Game::init(){
#ifdef DEBUG
printf("Game::init\n");
#endif
running = true;
}
void Game::initPlayer(){
#ifdef DEBUG
printf("Game::initPlayer\n");
#endif
}
void Game::editor(){
#ifdef DEBUG
printf("Game::editor\n");
#endif
}
void Game::run(){
#ifdef DEBUG
printf("Game::run\n");
#endif
running = false;
}
Code: Select all
App::init
App::run(splash)
App::run(title)
App::run(game)
Game::init
Game::initPlayer
Game::editor
Makefile
Code: Select all
#SOME LINES
CFLAGS = -O2 -G0 -Wall -Wno-write-strings -DDEBUG
#SOME LINES
LIBS = -lSDLmain -lstdc++ -lc
#SOME LINES
What's wrong??? Why this program throws only half of an output??? I have no idea!
And, what makes the case even more complicated THE SAME CODE RUNS PERFECTLY on my Ubuntu g++!!! I begin to get tired with that PSPSDK pointless errors...
Output using G++ compiler on Ubuntu
Code: Select all
App::init
App::run(splash)
App::run(title)
App::run(game)
Game::init
Game::initPlayer
Game::editor
Game::run
App::run(exit)
App::end