forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

I little hint please...

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
F34R



Joined: 28 Jul 2005
Posts: 29

PostPosted: Sun May 21, 2006 7:12 am    Post subject: I little hint please... Reply with quote

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
View user's profile Send private message
ReKleSS



Joined: 18 Jun 2005
Posts: 73
Location: Melbourne, Australia

PostPosted: Sun May 21, 2006 9:08 am    Post subject: Reply with quote

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
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Sun May 21, 2006 9:48 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
F34R



Joined: 28 Jul 2005
Posts: 29

PostPosted: Sun May 21, 2006 12:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Sun May 21, 2006 3:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Sun May 21, 2006 3:19 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Sun May 21, 2006 3:32 pm    Post subject: Reply with quote

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
View user's profile Send private message
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Sun May 21, 2006 3:50 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
cheriff
Regular


Joined: 23 Jun 2004
Posts: 262
Location: Sydney.au

PostPosted: Sun May 21, 2006 4:14 pm    Post subject: Reply with quote

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
View user's profile Send private message
ooPo
Site Admin


Joined: 17 Jan 2004
Posts: 2032
Location: Canada

PostPosted: Sun May 21, 2006 4:53 pm    Post subject: Reply with quote

Code:
gcc -std=c99

Simple!
Back to top
View user's profile Send private message Visit poster's website
groepaz



Joined: 01 Sep 2005
Posts: 305

PostPosted: Sun May 21, 2006 5:31 pm    Post subject: Reply with quote

ofcourse you should use
Code:
gcc --ansi --strict --pedantic


damn newschoolers :o)
_________________
http://www.hitmen-console.org
http://hitmen.c02.at/files/yapspd/
Back to top
View user's profile Send private message Visit poster's website
F34R



Joined: 28 Jul 2005
Posts: 29

PostPosted: Mon May 22, 2006 12:06 am    Post subject: ok Reply with quote

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
View user's profile Send private message
adrahil



Joined: 16 Mar 2006
Posts: 277

PostPosted: Mon May 22, 2006 3:56 am    Post subject: Reply with quote

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
View user's profile Send private message
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Mon May 22, 2006 8:58 am    Post subject: Reply with quote

that is correct
_________________
10011011 00101010 11010111 10001001 10111010
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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