| View previous topic :: View next topic |
| Author |
Message |
Marach
Joined: 24 May 2009 Posts: 31
|
Posted: Thu Jun 04, 2009 4:29 am Post subject: Writing text to files - fixed |
|
|
I'm writing my small homebrew game.
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: | #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;
} |
app.cpp
| Code: | #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
} |
game.h
| Code: | #ifndef GAME_H
#define GAME_H
#include "app.h"
struct Game {
bool running;
//----------//
void init();
void initPlayer();
void editor();
void run();
};
#endif // GAME_H |
game.cpp
| Code: | #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;
} |
Output
| Code: | App::init
App::run(splash)
App::run(title)
App::run(game)
Game::init
Game::initPlayer
Game::editor |
If it matters, the output file has 98 bytes.
Makefile
| Code: | #SOME LINES
CFLAGS = -O2 -G0 -Wall -Wno-write-strings -DDEBUG
#SOME LINES
LIBS = -lSDLmain -lstdc++ -lc
#SOME LINES |
Skipped files: appspec.h, appspec.cpp, app.h
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: | App::init
App::run(splash)
App::run(title)
App::run(game)
Game::init
Game::initPlayer
Game::editor
Game::run
App::run(exit)
App::end |
And that's good. _________________ Why my REAL email adress has been BANNED???
marach5 (at) gmail (dot) com ???
Last edited by Marach on Fri Jun 05, 2009 9:51 pm; edited 1 time in total |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Thu Jun 04, 2009 6:38 am Post subject: |
|
|
can you post a .tar.gz of your code somewhere?
What you posted here isn't complete so it's hard to debug (we're missing app.h, at least). |
|
| Back to top |
|
 |
Marach
Joined: 24 May 2009 Posts: 31
|
Posted: Thu Jun 04, 2009 3:57 pm Post subject: |
|
|
Ok, I will upload it to my website, but I'm posting from PSP right now :/
Ready! if you want it, here you are:
http://elementgame.boo.pl/downloads/public/Tilted.tar.gz _________________ Why my REAL email adress has been BANNED???
marach5 (at) gmail (dot) com ??? |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Thu Jun 04, 2009 4:19 pm Post subject: |
|
|
| Printing to a file doesn't immediately write to the file. It goes to a cache. You need to flush the file, then wait a few seconds or the print may be lost. Ran into that myself once - the log file didn't go anywhere nearly as far as I could SEE the program going because the PSP would hang before the log data could actually be written to the log file. |
|
| Back to top |
|
 |
Marach
Joined: 24 May 2009 Posts: 31
|
Posted: Thu Jun 04, 2009 5:18 pm Post subject: |
|
|
BUT.
I after opening the text file, I wrote
| Code: | | setbuf(stdout, NULL); |
to prevent saving to cache first.
I even tried writing
to flush the cache what already doesn't exist!
I will try waiting some seconds to save that data...
So the verdict is:
"File handling in PSP is crappy."
My Meritous port has some problem with files too! But saving is much more important than some debug info...
BTW, I managed to use MinPSPW in Ubuntu with Code::Blocks =D _________________ Why my REAL email adress has been BANNED???
marach5 (at) gmail (dot) com ??? |
|
| Back to top |
|
 |
Marach
Joined: 24 May 2009 Posts: 31
|
Posted: Thu Jun 04, 2009 9:58 pm Post subject: |
|
|
OMG it's crazy...
I changed the final cleanup function:
| Code: | void App::end(){
#ifdef DEBUG
printf("App::end\n");
fflush(stdout);
SDL_Delay(5000);
#endif
} |
5000 = 5 seconds.
And guess what? File stays at 98 kB! It changes nothing. Any MORE ideas? xD
SORRY, it's 98 bytes :/ _________________ Why my REAL email adress has been BANNED???
marach5 (at) gmail (dot) com ??? |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Fri Jun 05, 2009 4:24 am Post subject: |
|
|
| Uh, you misunderstood me - you need to fflush and wait after EVERY SINGLE WRITE. If it hangs, you lose the data right then... you'll never get to the cleanup code. |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Fri Jun 05, 2009 5:31 am Post subject: |
|
|
| You probably need to actually close the file when you're done writing to it. fflush flushes application buffers but never guarantees that data will hit the media. I don't know whether the PSP kernel has a fsync() equivalent, but we know from experience that flushing, closing the file, and then waiting a short while should work. |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Fri Jun 05, 2009 7:32 am Post subject: |
|
|
| Yeah, probably should for each debug print: reopen std for append, write, close, then wait a few secs. |
|
| Back to top |
|
 |
Marach
Joined: 24 May 2009 Posts: 31
|
Posted: Fri Jun 05, 2009 6:29 pm Post subject: |
|
|
The app doesn't hang, it just peacefully quits to XMB. I will try to fflush every write and close the file AND wait 5 seconds, let's see if it works... _________________ Why my REAL email adress has been BANNED???
marach5 (at) gmail (dot) com ??? |
|
| Back to top |
|
 |
Marach
Joined: 24 May 2009 Posts: 31
|
Posted: Fri Jun 05, 2009 9:53 pm Post subject: |
|
|
YAAAY xD
Thanks guys, it's working :)
I removed setbuf(NULL); and added fflush(stdout); after every printf, then added fclose(stdout); on cleanup function.
BUT that does not change the verdict that file handling in PSP is crappy. _________________ Why my REAL email adress has been BANNED???
marach5 (at) gmail (dot) com ??? |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Fri Jun 05, 2009 10:38 pm Post subject: |
|
|
I think your expectations are wrong. The lib code seems to me to comply with the C standard.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Marach
Joined: 24 May 2009 Posts: 31
|
Posted: Fri Jun 05, 2009 10:58 pm Post subject: |
|
|
The same code is working on my PC and not working on my PSP, that clearly states that something is wrong with AT LEAST exit callbacks - it's not that hard to wait for saving to complete! My PC somehow does it...
EDIT: Only C++, cstdlib and SDL, of course. _________________ Why my REAL email adress has been BANNED???
marach5 (at) gmail (dot) com ??? |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Fri Jun 05, 2009 11:05 pm Post subject: |
|
|
Both can be compliant and behave differently.
The fact your PC is line buffered and stuff appears on the console right away is irrelevant.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Marach
Joined: 24 May 2009 Posts: 31
|
Posted: Fri Jun 05, 2009 11:55 pm Post subject: |
|
|
Idon't write to console, I reopen stdout to write always to text file. It's shorter then that Fprintf. _________________ Why my REAL email adress has been BANNED???
marach5 (at) gmail (dot) com ??? |
|
| Back to top |
|
 |
jsharrad
Joined: 20 Oct 2005 Posts: 102
|
Posted: Sat Jun 06, 2009 4:19 am Post subject: |
|
|
| Your pc doesn't reboot every time a program ends, it needs to do some cleanup... the PSP does. If you want a "clean" exit, you have to code it yourself. |
|
| Back to top |
|
 |
NoEffex
Joined: 27 Nov 2008 Posts: 108
|
Posted: Sat Jun 06, 2009 9:02 am Post subject: |
|
|
| jsharrad wrote: | | Your pc doesn't reboot every time a program ends, it needs to do some cleanup... the PSP does. If you want a "clean" exit, you have to code it yourself. |
If it's a plugin, you'd do
int status;
sceKernelStopUnloadSelfModule(0, NULL, &status, NULL);
However, if it's an eboot, then you'd be correct :). _________________ Programming with:
Geany + Latest PSPSDK from svn |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Sat Jun 06, 2009 10:14 am Post subject: |
|
|
| A lot of conversions to consoles like the PSP don't even bother cleaning up. Where the PC version calls a bunch of cleanup code, the console version just exits. Depending on the console, it may not even do that. My conversion of Wolf3D for the 32X doesn't even exit much less clean up. If you want to quit, you turn off the SEGA. |
|
| Back to top |
|
 |
|