I little hint please...

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
F34R
Posts: 29
Joined: Thu Jul 28, 2005 1:11 pm

I little hint please...

Post by F34R »

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: Select all

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdio.h>
#include "graphics.h"
#include "callback.h"

PSP_MODULE_INFO&#40;"Homework", 0, 1, 1&#41;;

#define RGB&#40;r, g, b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<16&#41;|&#40;&#40;b&#41;<<16&#41;&#41;


int main&#40;&#41; &#123;

          SetupCallbacks&#40;&#41;;
          initGraphics&#40;&#41;;
          SceCtrlData pad;
          int goodScore;
          int badScore;
          Color white = RGB&#40;255,255,255&#41;;
          Color green = RGB&#40;0,255,0&#41;;
          char PrintScoreG&#91;200&#93;;

          for&#40;;;&#41; &#123;
          goodScore = 0;
          badScore = 0;
          printTextScreen&#40;20, 5, "Can you add ?", white&#41;;
          printTextScreen&#40;20, 15, "Coded By F34R", white&#41;;
          printTextScreen&#40;10, 40, "1 + 1 =", white&#41;;
          printTextScreen&#40;10, 60, "CROSS&#58; 0", white&#41;;
          printTextScreen&#40;10, 70, "SQUARE&#58; 3", white&#41;;
          printTextScreen&#40;10, 80, "TRIANGLE&#58; 21", white&#41;;
          printTextScreen&#40;10, 90, "CIRCLE&#58; 2", white&#41;;

             sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
             if&#40;pad.Buttons & PSP_CTRL_CROSS&#41; &#123;
                printTextScreen&#40;10,120, "CORRECT", green&#41;;
                goodScore++;
                printTextScreen&#40;10, 150, "This program will exit now.", white&#41;;
                break;
             &#125;
             if&#40;pad.Buttons & PSP_CTRL_SQUARE&#41; &#123;
               printTextScreen&#40;10,120, "INCORRECT", green&#41;;
               goodScore--;
             &#125;
             if&#40;pad.Buttons & PSP_CTRL_TRIANGLE&#41; &#123;
               printTextScreen&#40;10,120, "INCORRECT", green&#41;;
               goodScore--;
             &#125;
             if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; &#123;
               printTextScreen&#40;10,120, "INCORRECT", green&#41;;
               goodScore--;
             &#125;

          sprintf&#40;PrintScoreG, " Correct &#58; %i", goodScore&#41;;
          printTextScreen&#40;10, 200, PrintScoreG, green&#41;;
          flipScreen&#40;&#41;;
          &#125;
sceKernelExitGame&#40;&#41;;
return 0;
&#125;
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.
User avatar
ReKleSS
Posts: 73
Joined: Sat Jun 18, 2005 12:57 pm
Location: Melbourne, Australia

Post by ReKleSS »

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
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

If it's C, it won't compile. This is the second time I've seen this

Code: Select all

int main&#40;&#41; &#123; 
          SetupCallbacks&#40;&#41;; 
          initGraphics&#40;&#41;; 
          SceCtrlData pad;
this week.
In C you must declare all your variables before calling any functions, ie.

Code: Select all

int main&#40;&#41; &#123; 
          SceCtrlData pad;
          SetupCallbacks&#40;&#41;; 
          initGraphics&#40;&#41;; 
Jim
F34R
Posts: 29
Joined: Thu Jul 28, 2005 1:11 pm

Post by F34R »

It compiles without any warnings or errors. When I run it on the PSP, the only text I see is the output from:

Code: Select all

sprintf&#40;PrintScoreG, " Correct &#58; %i", goodScore&#41;;
          printTextScreen&#40;10, 200, PrintScoreG, green&#41;; 
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.
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Jim wrote:If it's C, it won't compile. This is the second time I've seen this

Code: Select all

int main&#40;&#41; &#123; 
          SetupCallbacks&#40;&#41;; 
          initGraphics&#40;&#41;; 
          SceCtrlData pad;
this week.
In C you must declare all your variables before calling any functions, ie.

Code: Select all

int main&#40;&#41; &#123; 
          SceCtrlData pad;
          SetupCallbacks&#40;&#41;; 
          initGraphics&#40;&#41;; 
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.
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

moonlight wrote:
Jim wrote:If it's C, it won't compile. This is the second time I've seen this

Code: Select all

int main&#40;&#41; &#123; 
          SetupCallbacks&#40;&#41;; 
          initGraphics&#40;&#41;; 
          SceCtrlData pad;
this week.
In C you must declare all your variables before calling any functions, ie.

Code: Select all

int main&#40;&#41; &#123; 
          SceCtrlData pad;
          SetupCallbacks&#40;&#41;; 
          initGraphics&#40;&#41;; 
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
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Drakonite wrote:
moonlight wrote:
Jim wrote:If it's C, it won't compile. This is the second time I've seen this

Code: Select all

int main&#40;&#41; &#123; 
          SetupCallbacks&#40;&#41;; 
          initGraphics&#40;&#41;; 
          SceCtrlData pad;
this week.
In C you must declare all your variables before calling any functions, ie.

Code: Select all

int main&#40;&#41; &#123; 
          SceCtrlData pad;
          SetupCallbacks&#40;&#41;; 
          initGraphics&#40;&#41;; 
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?
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

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
cheriff
Regular
Posts: 258
Joined: Wed Jun 23, 2004 5:35 pm
Location: Sydney.au

Post by cheriff »

from the gcc manpage:

Code: Select all

gnu89
     Default, ISO C90 plus GNU extensions &#40;including some C99 fea-
     tures&#41;
I'm guessing free mixing of declarations and code is one of the c99 features included :)
Damn, I need a decent signature!
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Code: Select all

gcc -std=c99
Simple!
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

ofcourse you should use

Code: Select all

gcc --ansi --strict --pedantic
damn newschoolers :o)
F34R
Posts: 29
Joined: Thu Jul 28, 2005 1:11 pm

ok

Post by F34R »

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: Select all

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdio.h>
#include "graphics.h"
#include "callback.h"

PSP_MODULE_INFO&#40;"Homework", 0, 1, 1&#41;;

#define RGB&#40;r, g, b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;


int main&#40;&#41; &#123;

          SceCtrlData pad;
          int goodScore;
          int badScore;
          int i;
          Color white = RGB&#40;255,255,255&#41;;
          Color green = RGB&#40;0,255,0&#41;;
          char PrintScoreG&#91;50&#93;;
          SetupCallbacks&#40;&#41;;
          initGraphics&#40;&#41;;


          for&#40;;;&#41; &#123;
          goodScore = 0;
          badScore = 0;
          printTextScreen&#40;20, 5, "Can you add ?", white&#41;;
          printTextScreen&#40;20, 15, "Coded By F34R", white&#41;;
          printTextScreen&#40;10, 40, "1 + 1 =", white&#41;;
          printTextScreen&#40;10, 60, "CROSS&#58; 0", white&#41;;
          printTextScreen&#40;10, 70, "SQUARE&#58; 3", white&#41;;
          printTextScreen&#40;10, 80, "TRIANGLE&#58; 21", white&#41;;
          printTextScreen&#40;10, 90, "CIRCLE&#58; 2", white&#41;;

          sceCtrlReadBufferPositive&#40;&pad, 1&#41;;

             if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; &#123;
                printTextScreen&#40;10,120, "CORRECT", green&#41;;
                goodScore++;
                printTextScreen&#40;10, 150, "Press Start to exit.", white&#41;;

             &#125;
          sprintf&#40;PrintScoreG, " Correct &#58; %i", goodScore&#41;;
          printTextScreen&#40;10, 200, PrintScoreG, green&#41;;
             flipScreen&#40;&#41;;
             for&#40;i=0; i<1; i++&#41; &#123;
                              sceDisplayWaitVblankStart&#40;&#41;;
                    &#125;


          if&#40;pad.Buttons & PSP_CTRL_START&#41; &#123;
            break;
          &#125;
          &#125;
          

sceKernelExitGame&#40;&#41;;
return 0;
          &#125;
adrahil
Posts: 274
Joined: Thu Mar 16, 2006 1:55 am

Post by adrahil »

Code: Select all

          for&#40;;;&#41; &#123;
          goodScore = 0;
//
//&#91;...&#93;
//
             if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; &#123;
                printTextScreen&#40;10,120, "CORRECT", green&#41;;
                goodScore++;
                printTextScreen&#40;10, 150, "Press Start to exit.", white&#41;;

             &#125;
//
//&#91;...&#93;
//
          &#125;
          
Uhm, I might not be an expert in C, but don't you always initialise that variable to 0 at each loop?
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

that is correct
10011011 00101010 11010111 10001001 10111010
Post Reply