 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
F34R
Joined: 28 Jul 2005 Posts: 29
|
Posted: Sun May 21, 2006 7:12 am Post subject: I little hint please... |
|
|
Just started to learn C, and I wanted to start very simple. What I thought wouldnt be too difficult, has turned into a hair pulling nightmare for a n00b. I would appreciate a hint on this, I'm not really looking for an exact answer here. Just a hint where I've went wrong. Here is the code:
| Code: |
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdio.h>
#include "graphics.h"
#include "callback.h"
PSP_MODULE_INFO("Homework", 0, 1, 1);
#define RGB(r, g, b) ((r)|((g)<<16)|((b)<<16))
int main() {
SetupCallbacks();
initGraphics();
SceCtrlData pad;
int goodScore;
int badScore;
Color white = RGB(255,255,255);
Color green = RGB(0,255,0);
char PrintScoreG[200];
for(;;) {
goodScore = 0;
badScore = 0;
printTextScreen(20, 5, "Can you add ?", white);
printTextScreen(20, 15, "Coded By F34R", white);
printTextScreen(10, 40, "1 + 1 =", white);
printTextScreen(10, 60, "CROSS: 0", white);
printTextScreen(10, 70, "SQUARE: 3", white);
printTextScreen(10, 80, "TRIANGLE: 21", white);
printTextScreen(10, 90, "CIRCLE: 2", white);
sceCtrlReadBufferPositive(&pad, 1);
if(pad.Buttons & PSP_CTRL_CROSS) {
printTextScreen(10,120, "CORRECT", green);
goodScore++;
printTextScreen(10, 150, "This program will exit now.", white);
break;
}
if(pad.Buttons & PSP_CTRL_SQUARE) {
printTextScreen(10,120, "INCORRECT", green);
goodScore--;
}
if(pad.Buttons & PSP_CTRL_TRIANGLE) {
printTextScreen(10,120, "INCORRECT", green);
goodScore--;
}
if(pad.Buttons & PSP_CTRL_CIRCLE) {
printTextScreen(10,120, "INCORRECT", green);
goodScore--;
}
sprintf(PrintScoreG, " Correct : %i", goodScore);
printTextScreen(10, 200, PrintScoreG, green);
flipScreen();
}
sceKernelExitGame();
return 0;
}
|
That is supposed to display a question, and respond when the user presses a button. Any help would be appreciated. I'd like to learn why it doesnt work, so I'd like a hint please. Thanks for your time. |
|
| Back to top |
|
 |
ReKleSS

Joined: 18 Jun 2005 Posts: 73 Location: Melbourne, Australia
|
Posted: Sun May 21, 2006 9:08 am Post subject: |
|
|
It would help if you actually explained why it doesn't work, i.e. what happens and what goes wrong. After looking over it quickly, I don't see anything problematic in the code you've given.
-ReK |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Sun May 21, 2006 9:48 am Post subject: |
|
|
If it's C, it won't compile. This is the second time I've seen this
| Code: | int main() {
SetupCallbacks();
initGraphics();
SceCtrlData pad; |
this week.
In C you must declare all your variables before calling any functions, ie.
| Code: | int main() {
SceCtrlData pad;
SetupCallbacks();
initGraphics(); |
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
F34R
Joined: 28 Jul 2005 Posts: 29
|
Posted: Sun May 21, 2006 12:13 pm Post subject: |
|
|
It compiles without any warnings or errors. When I run it on the PSP, the only text I see is the output from:
| Code: |
sprintf(PrintScoreG, " Correct : %i", goodScore);
printTextScreen(10, 200, PrintScoreG, green);
|
Nothing happens at all if I press any buttons. If I use Home > yes to exit, it crashes the PSP, and it powers itself off.
After looking at what the poster above said about decalaring variables before calling functions, I changed it. Now it actually works. Thanks. I didnt realize that, and now I've learned.... thank you very much.
On a side note, it did compile the other way though lol. |
|
| Back to top |
|
 |
moonlight
Joined: 26 Oct 2005 Posts: 567
|
Posted: Sun May 21, 2006 3:08 pm Post subject: |
|
|
| Jim wrote: | If it's C, it won't compile. This is the second time I've seen this
| Code: | int main() {
SetupCallbacks();
initGraphics();
SceCtrlData pad; |
this week.
In C you must declare all your variables before calling any functions, ie.
| Code: | int main() {
SceCtrlData pad;
SetupCallbacks();
initGraphics(); |
Jim |
Incorrect. That changed in the new C standard of 1999 if i don't remember bad. Since that, variables can be declared in whatever part of the function. |
|
| Back to top |
|
 |
Drakonite Site Admin

Joined: 17 Jan 2004 Posts: 989
|
Posted: Sun May 21, 2006 3:19 pm Post subject: |
|
|
| moonlight wrote: | | Jim wrote: | If it's C, it won't compile. This is the second time I've seen this
| Code: | int main() {
SetupCallbacks();
initGraphics();
SceCtrlData pad; |
this week.
In C you must declare all your variables before calling any functions, ie.
| Code: | int main() {
SceCtrlData pad;
SetupCallbacks();
initGraphics(); |
Jim |
Incorrect. That changed in the new C standard of 1999 if i don't remember bad. Since that, variables can be declared in whatever part of the function. |
Erm.. Incorrrect! .. while C99 does in fact change that, it's still against the old ansi C rules, which many compilers (gcc for one IIRC) default to unless you specify otherwise. _________________ Shoot Pixels Not People!
Makeshift Development |
|
| Back to top |
|
 |
moonlight
Joined: 26 Oct 2005 Posts: 567
|
Posted: Sun May 21, 2006 3:32 pm Post subject: |
|
|
| Drakonite wrote: | | moonlight wrote: | | Jim wrote: | If it's C, it won't compile. This is the second time I've seen this
| Code: | int main() {
SetupCallbacks();
initGraphics();
SceCtrlData pad; |
this week.
In C you must declare all your variables before calling any functions, ie.
| Code: | int main() {
SceCtrlData pad;
SetupCallbacks();
initGraphics(); |
Jim |
Incorrect. That changed in the new C standard of 1999 if i don't remember bad. Since that, variables can be declared in whatever part of the function. |
Erm.. Incorrrect! .. while C99 does in fact change that, it's still against the old ansi C rules, which many compilers (gcc for one IIRC) default to unless you specify otherwise. |
Oh i see. You are right. I thought that gcc followed by default the C99 since it lets me declare the variables in whatever part and it doesn't give me any errors.
Any ideas why gcc let to do that if not specyfing the C99 standard? |
|
| Back to top |
|
 |
Drakonite Site Admin

Joined: 17 Jan 2004 Posts: 989
|
Posted: Sun May 21, 2006 3:50 pm Post subject: |
|
|
| moonlight wrote: |
Oh i see. You are right. I thought that gcc followed by default the C99 since it lets me declare the variables in whatever part and it doesn't give me any errors.
Any ideas why gcc let to do that if not specyfing the C99 standard? |
Well, I suppose it might not be using the old strict ansi C by default then... It might be using lax rules or might be even be defaulting to C99... TBH I'm not sure what standard newer gcc's default to. _________________ Shoot Pixels Not People!
Makeshift Development |
|
| Back to top |
|
 |
cheriff Regular
Joined: 23 Jun 2004 Posts: 262 Location: Sydney.au
|
Posted: Sun May 21, 2006 4:14 pm Post subject: |
|
|
from the gcc manpage:
| Code: | gnu89
Default, ISO C90 plus GNU extensions (including some C99 fea-
tures) |
I'm guessing free mixing of declarations and code is one of the c99 features included :) _________________ Damn, I need a decent signature! |
|
| Back to top |
|
 |
ooPo Site Admin
Joined: 17 Jan 2004 Posts: 2032 Location: Canada
|
Posted: Sun May 21, 2006 4:53 pm Post subject: |
|
|
Simple! |
|
| Back to top |
|
 |
groepaz

Joined: 01 Sep 2005 Posts: 305
|
|
| Back to top |
|
 |
F34R
Joined: 28 Jul 2005 Posts: 29
|
Posted: Mon May 22, 2006 12:06 am Post subject: ok |
|
|
Here is the code once more. It works fine except that the score at the bottom isn't updated when the correct answer is chosen.
| Code: |
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdio.h>
#include "graphics.h"
#include "callback.h"
PSP_MODULE_INFO("Homework", 0, 1, 1);
#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))
int main() {
SceCtrlData pad;
int goodScore;
int badScore;
int i;
Color white = RGB(255,255,255);
Color green = RGB(0,255,0);
char PrintScoreG[50];
SetupCallbacks();
initGraphics();
for(;;) {
goodScore = 0;
badScore = 0;
printTextScreen(20, 5, "Can you add ?", white);
printTextScreen(20, 15, "Coded By F34R", white);
printTextScreen(10, 40, "1 + 1 =", white);
printTextScreen(10, 60, "CROSS: 0", white);
printTextScreen(10, 70, "SQUARE: 3", white);
printTextScreen(10, 80, "TRIANGLE: 21", white);
printTextScreen(10, 90, "CIRCLE: 2", white);
sceCtrlReadBufferPositive(&pad, 1);
if(pad.Buttons & PSP_CTRL_CIRCLE) {
printTextScreen(10,120, "CORRECT", green);
goodScore++;
printTextScreen(10, 150, "Press Start to exit.", white);
}
sprintf(PrintScoreG, " Correct : %i", goodScore);
printTextScreen(10, 200, PrintScoreG, green);
flipScreen();
for(i=0; i<1; i++) {
sceDisplayWaitVblankStart();
}
if(pad.Buttons & PSP_CTRL_START) {
break;
}
}
sceKernelExitGame();
return 0;
}
|
|
|
| Back to top |
|
 |
adrahil
Joined: 16 Mar 2006 Posts: 277
|
Posted: Mon May 22, 2006 3:56 am Post subject: |
|
|
| Code: | for(;;) {
goodScore = 0;
//
//[...]
//
if(pad.Buttons & PSP_CTRL_CIRCLE) {
printTextScreen(10,120, "CORRECT", green);
goodScore++;
printTextScreen(10, 150, "Press Start to exit.", white);
}
//
//[...]
//
}
|
Uhm, I might not be an expert in C, but don't you always initialise that variable to 0 at each loop? |
|
| Back to top |
|
 |
dot_blank

Joined: 28 Sep 2005 Posts: 498 Location: Brasil
|
Posted: Mon May 22, 2006 8:58 am Post subject: |
|
|
that is correct _________________ 10011011 00101010 11010111 10001001 10111010 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|