C/C++ Programming Help Thread
This is a discussion on C/C++ Programming Help Thread within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; Beat_Bob - In your position, accessing a method or variable of some object declared as the following will cause your ...
-
04-14-2010, 06:27 PM #10081words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
Beat_Bob - In your position, accessing a method or variable of some object declared as the following will cause your problem.
If a constructor of the object has no parameters, you should NOT use parenthesis in it's declaration, write it rather as:Code:player blah(); // bad definition ... blah.method(); // this line will raise the error blah.X = 10; // and this...
Code:player blah; ... blah.method(); blah.X = 10;

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-14-2010, 06:31 PM #10082QJ Gamer Gold

- Registriert seit
- Jul 2005
- Ort
- everywhere
- Beiträge
- 3.526
- Points
- 17.453
- Level
- 84
- Downloads
- 1
- Uploads
- 0
beat_bob, this is one that you could actually test with codepad itself:
C++ code - 18 lines - codepad
hope this helps
edit: sg beat me=-(1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been
-
04-14-2010, 06:35 PM #10083words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
damn right I beat you just like last night ohhhhhhh :humped:

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-14-2010, 06:39 PM #10084QJ Gamer Gold

- Registriert seit
- Jul 2005
- Ort
- everywhere
- Beiträge
- 3.526
- Points
- 17.453
- Level
- 84
- Downloads
- 1
- Uploads
- 0
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been
-
04-14-2010, 06:58 PM #10085QJ Gamer Green
- Registriert seit
- Jul 2007
- Beiträge
- 88
- Points
- 3.606
- Level
- 37
- Downloads
- 0
- Uploads
- 0
In the function:
extern void fillScreenRect(Color color, int x0, int y0, int width, int height);
What would I place for the color parameter?
Lets say I wanted..red for example?
Would it be a string? "FF0000"
-
04-14-2010, 07:05 PM #10086QJ Gamer Gold

- Registriert seit
- Jul 2005
- Ort
- everywhere
- Beiträge
- 3.526
- Points
- 17.453
- Level
- 84
- Downloads
- 1
- Uploads
- 0
no, u'd pass in a Color struct
IIRC, something like:
Color c(255,0,0,255);
fillScreenRect(c, 0,0,100,100);
but it's been a long time since i've used graphics.h, so might not be correct1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been
-
04-14-2010, 07:13 PM #10087QJ Gamer Green
- Registriert seit
- Jul 2007
- Beiträge
- 88
- Points
- 3.606
- Level
- 37
- Downloads
- 0
- Uploads
- 0
Oh okay thanks.
Well also I've got yet another error:
C++ code - 23 lines - codepadCode:player.cpp: In function 'void Step()': player.cpp:9: error: 'x' was not declared in this scope player.cpp:10: error: 'y' was not declared in this scope
What's the dillio?
-
04-14-2010, 07:27 PM #10088words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
You need to declare that Step is in the player namespace somehow to access the variable within that namespace.
Code:void player::Step() { blah blah }
...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-14-2010, 07:58 PM #10089Developer
- Registriert seit
- Jul 2008
- Ort
- Grand Rapids! MI
- Beiträge
- 125
- Points
- 8.092
- Level
- 60
- Downloads
- 5
- Uploads
- 2
thats why the buffer there has extra space so there wont be no overflow its simply security and if that fails then no big just bring the size up more to a reasonable about but for simple debug text you should have a problem with overflow (unless i guess it had a bug in the program where it trys loading more then you want to into the buffer then i guess there can be a overflow) but i never had a problem with it its kinda basic math each letter or symbol would equal one byte you set the size of the buffer in bytes so you should always but a few extra bytes into the buffer then logically needed to cope with over flow. now for the function you answered my question and yes stdlib is part of the SDK just not the PSPSDK that file is imported to help the PSPSDK to compile and thats still pretty much answered my question what file it is in
-
04-14-2010, 08:12 PM #10090QJ Gamer Green
- Registriert seit
- Jul 2007
- Beiträge
- 88
- Points
- 3.606
- Level
- 37
- Downloads
- 0
- Uploads
- 0
Now I get this:
C++ code - 62 lines - codepadCode:main.cpp: In function 'int main()': main.cpp:32: error: initializer expression list treated as compound expression main.cpp:32: warning: left-hand operand of comma has no effect main.cpp:32: warning: right-hand operand of comma has no effect main.cpp:32: warning: right-hand operand of comma has no effect main.cpp:34: warning: statement is a reference, not call, to function 'fillScr nRect' main.cpp:34: warning: statement has no effect main.cpp:34: warning: left-hand operand of comma has no effect main.cpp:34: warning: right-hand operand of comma has no effect main.cpp:34: warning: right-hand operand of comma has no effect main.cpp:34: warning: right-hand operand of comma has no effect
-
04-14-2010, 08:32 PM #10091QJ Gamer Gold

- Registriert seit
- Jul 2005
- Ort
- everywhere
- Beiträge
- 3.526
- Points
- 17.453
- Level
- 84
- Downloads
- 1
- Uploads
- 0
alright, i looked up the graphics.h, in graphics.h underneath:
add:Code:#define A(color) ((u8)(color >> 24 & 0xFF)) #define B(color) ((u8)(color >> 16 & 0xFF)) #define G(color) ((u8)(color >> 8 & 0xFF)) #define R(color) ((u8)(color & 0xFF))
then it'll workCode:#define RGBA(r,g,b,a) ((r)<<24|(g)<<16|(b)<<8|a)
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been
-
04-14-2010, 08:34 PM #10092words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
lilmnm - That extra space is a technical waste despite how virtually minuscule it is since you are expecting a 3 digit number tops yet you're compensating for some 7 digit number. Apply that to something with a substantial size like a texture or uncompressed audio and that won't work. Besides, when an exploiter pokes around and makes the sceGetCpuClockSpeed or w/e function return a large 32 bit integer value (10,000,000+) your buffer will overflow and you will get undefined behavior as a result.
These Saved Game exploits and the infamous Twilight Hack in Zelda on the Wii are all caused by programmers that do **** just like that. You get to name your horse in Zelda Twilight Princess. Max characters are something like 15. "Cool we'll just have the OSK max out the character entry at 15." Logically there is no way to make it overflow in code. Well when the saved data got hacked and the horse name changed to "asdbasfjaksdjfksjdfksajd fksjfksjfiwjef" it overflowed the buffer and oh geez the Wii is now a piracy machine. Look at the libtiff exploit NUMEROUS times on PSP, GTA exploit, Lumines exploit, Gripshift exploit, the list goes on. Hell even the KXploit if you have been around long enough for that. Did you know PacMan does this as well? They use an unsigned char as the level variable. An unsigned char can hold at max 256 values (starting at 0) so when you get to the 256th level it overflows and the undefined behavior that occurs is half hte level is distorted to hell. Those are all stupid programming mistakes that a simple snprint instead of sprint (or something similar obviously, depends on the data type) would've prevented. It reads in the name from the saved data file and puts it into the 'name' buffer, if you will. If it doesn't limit the name to the size of the buffer, you get your overflow.
It might not be so black and white for integers but like so many exploits involving literal strings there is no limit to the length to receive. You can size that char array up all you want until it corrupts the stack for all I care but I could just keep appending 'asdfasdf'.
There is no safer way to prevent undefined behavior then to prevent the processes that cause it.
sprintf reads the formatted string buffer into the destination buffer.
snprintf will read, at most, 'n' number of bytes from the formatted string buffer into the destination buffer.
The whole point is to operate using good habits. These overflows might never happen if you use dynamically allocated variables such as STL containers but its still better to be safe then sorry. (I'm sure Sony management got pissed at GTA for what they did hah)Geändert von SG57 (04-14-2010 um 08:45 PM Uhr)

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-14-2010, 09:51 PM #10093QJ Gamer Green
- Registriert seit
- Jul 2007
- Beiträge
- 88
- Points
- 3.606
- Level
- 37
- Downloads
- 0
- Uploads
- 0
Got it.
Well i've got another problem (of course).
My while(1) loop seems to crash my PSP (I have to restart it)
C++ code - 48 lines - codepad
Even without that function in it, it still crashes.
All the function does is increase 2 members (x and y) of that class by 2.
-
04-14-2010, 10:16 PM #10094QJ Gamer Gold

- Registriert seit
- Jul 2005
- Ort
- everywhere
- Beiträge
- 3.526
- Points
- 17.453
- Level
- 84
- Downloads
- 1
- Uploads
- 0
it doesn't crash, it just never exits out of the loop, that's the problem
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been
-
04-14-2010, 11:05 PM #10095QJ Gamer Green
- Registriert seit
- Jul 2007
- Beiträge
- 88
- Points
- 3.606
- Level
- 37
- Downloads
- 0
- Uploads
- 0
How would I fix it?
-
04-14-2010, 11:41 PM #10096QJ Gamer Gold

- Registriert seit
- Jul 2005
- Ort
- everywhere
- Beiträge
- 3.526
- Points
- 17.453
- Level
- 84
- Downloads
- 1
- Uploads
- 0
don't go into an infinite loop
while(1) basically says keep doing whatever is in my {}'s until u say break, nothing well break out of it unless u call break, or make the while loop dependent on a variable that u can change i.e:
of course it'd be easier to use a for loop for what i demonstrated, but this is to show the concept, u could also do:Code:int i=0; bool done=false; while(!done){ i++; if(i==10) done=true; } printf("%d\n",i);
Code:int i=0; while(i){ i++; if(i==10) break; } printf("%d\n",i);1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been
-
04-15-2010, 01:40 AM #10097QJ Gamer Green
- Registriert seit
- Jun 2009
- Beiträge
- 61
- Points
- 2.003
- Level
- 27
- Downloads
- 0
- Uploads
- 0
-
04-15-2010, 10:53 AM #10098words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
LegendMythe - You would use the char buffer (not int buffer that he mistakenly used) with the print text screen function after formatting it.
Code:char text[9]; snprintf(text, 9, "CPU: %d", scePowerGetCpuClockFrequency()); printTextScreen(x, y, text, RGBA(255,255,255,255));

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-15-2010, 04:10 PM #10099QJ Gamer Green
- Registriert seit
- Jul 2007
- Beiträge
- 88
- Points
- 3.606
- Level
- 37
- Downloads
- 0
- Uploads
- 0
Okay thanks.
Also, how do I access members from outside of a class?
Ex:
I have a variable:
int x;
in my main.cpp file
Lets say I had a class in myclass.cpp, how would I access that variable 'x' ?
main.cpp:
int x;
myclass.cpp:
#include myclass.h
y = main.cpp.x;
How do I do it?
-
04-15-2010, 05:26 PM #10100Developer
- Registriert seit
- Jul 2008
- Ort
- Grand Rapids! MI
- Beiträge
- 125
- Points
- 8.092
- Level
- 60
- Downloads
- 5
- Uploads
- 2
-
04-15-2010, 08:29 PM #10101words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
Beat_Bob - What you want would involve using the 'extern' keyword and another source file and it gets ugly fast. You could wrap your main 'program' into a class of its own (normally called 'Game' or 'GameEngine') with a 'setX' function then have a function in your myclass function take the GameEngine class as a parameter and call 'setX' from that. Or you could have the 'myclass' class have a function that takes a pointer to an integer which then alters it that way, but really - it is exceptionally good design to make a class should directly manipulate its own members. So the variable 'x' should be within the 'myclass' class and that class should handle directly manipulating it. You can make a pair of 'helper' methods for that class to 'get' the x value for say drawing at that x position later on, you'd have a 'get' function in the 'myclass' class that would return a constant reference to it.
Then later on in main.cppCode:// myclass.h class myclass { private: int x; public: myclass() : x(0) {} void setX(const int& k) { x = k; } const int& getX() const { return x; } };
Code:myclass object; ... object.setX(240); ... printTextScreen(object.getX(), 0, "asdf", RGBA(0,0,0,0));

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-15-2010, 10:27 PM #10102QJ Gamer Gold

- Registriert seit
- Jul 2005
- Ort
- everywhere
- Beiträge
- 3.526
- Points
- 17.453
- Level
- 84
- Downloads
- 1
- Uploads
- 0
umm, sg, why are u using an &?, it's an single int....
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been
-
04-15-2010, 10:51 PM #10103words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
Using references in C++ is a good early habit to get into, though you are right since they are more for larger containers than an integer but still...

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-16-2010, 12:31 AM #10104QJ Gamer Green
- Registriert seit
- Jun 2009
- Beiträge
- 61
- Points
- 2.003
- Level
- 27
- Downloads
- 0
- Uploads
- 0
Ty but doesn't that has to be "sprintf" instead of "snprintf"?
and my program chrashes how should i use it like this?
Code:#include <psppower.h> #include <pspkernel.h> #include <pspdisplay.h> #include <pspctrl.h> #include "graphics.h" #include "callback.h" #include <stdio.h> int main () { SetupCallbacks(); initGraphics(); SceCtrlData pad, lastpad; sceCtrlReadBufferPositive(&lastpad, 1); while(1) { sceCtrlReadBufferPositive(&pad, 1); if(pad.Buttons != lastpad.Buttons) { lastpad = pad; if (pad.Buttons & PSP_CTRL_CROSS) { char text[9]; sprintf(text, 9, "CPU: %d", scePowerGetCpuClockFrequency()); printTextScreen(x, y, text, RGBA(255,255,255,255)); } } } return 0; }Geändert von LegendMythe (04-16-2010 um 03:06 AM Uhr)
-
04-16-2010, 07:43 AM #10105words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
LegendMythe -
We literally just got done talking about it lilmnm and I.. did you not read those posts? The reason its crashing is because you changed snprintf to sprintf but you didn't remove the second argument that is specifically for snprintf and that is how many bytes to read from the formatted string, so its actually expecting a pointer to the first character of the formatting string but getting an integer pointing to who knows where in temporary memory.
The reason the compiler won't catch this is because the variable argument symbol ... allows anything but sprintf expects a string and you as the programmer must give it what it wants (this wouldn't have happened if you would've just stayed with snprintf since it expects an integer as 2nd and would call an error if not supplied). Either use snprintf or remove the second argument from your current code.
--------
in an unrelated turn of events...
5,000th post! Just shy of 5 years, that means I'm making over a 1000 posts a year muaha.Geändert von SG57 (04-16-2010 um 07:56 AM Uhr)

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-16-2010, 08:28 AM #10106QJ Gamer Green
- Registriert seit
- Jun 2009
- Beiträge
- 61
- Points
- 2.003
- Level
- 27
- Downloads
- 0
- Uploads
- 0
-
04-16-2010, 08:44 AM #10107words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
04-16-2010, 08:58 AM #10108QJ Gamer Green
- Registriert seit
- Jun 2009
- Beiträge
- 61
- Points
- 2.003
- Level
- 27
- Downloads
- 0
- Uploads
- 0
-
04-16-2010, 07:02 PM #10109QJ Gamer Green
- Registriert seit
- Jul 2007
- Beiträge
- 88
- Points
- 3.606
- Level
- 37
- Downloads
- 0
- Uploads
- 0
Err not sure if that's what I meant.
This is what I want to do actually though.
Let's say I wanted to access my pad variable in main.cpp from my player.cpp file:
SceCtrlData pad;
So that way, I could make a function in player (a loop):
void Step(){
if(pad.Buttons & PSP_CTRL_WHATEVER){
//do something with player depending on pad's button state
}
}
And I could just call step in main.cpp.
However, pad does not exist in player.cpp, it is only local to main.cpp
So what would I do?
-
04-16-2010, 07:59 PM #10110words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
You would do:
I recommend having 3 main functions in your main source file so things don't get ugly once your game starts evolving. Input, Update and Draw. Each one handles only things that fit into their category. All input related code goes in Input, all input-related code is completely game specific and will probably never be re-used in another game. All 'logical' code from collision detection to a simple counter increment expression to advanced A.I.. All drawing related code goes in the Draw function. The Update and Drawing method will have code that can be re-used in other games. Then your main loop looks something like:Code:// main.cpp if (pad.Buttons & PSP_CTRL_WHATEVER) { player.Step(); }
Other naming patterns exist, such as Input, Processing and Output (since that is what you're doing essentially) and HandleEvents, Think, Display (also what you're doing). I prefer the code I showed for no particular reason, I guess because I devised it on my own years ago before reading a single game design article. I was sick of messy code so I split it into those 3.Code:while(1) { Input(); Update(); Draw(); }
Anyways, Classes should always try and be both platform and game independent. This allows for better code re-usability and it helps break your game up into smaller more manageable chunks allowing you to add to your game easier. The idea is that you should be able to take the Ball class from a Pong game and use it in a Breakout game with little to no change since their functionality is practically the same. 9 times out of 10 you are better off re-writing the game's source from scratch anyways but helpful classes and templates are almost always brought over. For example, I have a Fader class I use for transitions to fade the screen to black or white.
Now obviously there will be a point at which a class object is too game specific and it can't be re-used (such as an Enemy class, most Enemies aren't too similar from game to game when we talk about AI, scripting, abilities, etc.). Since all Input is game specific, putting game specific code in a non-game specific class is counter intuitive and defeats the whole purpose... Instead, make a method that carries out what you want (ie, Step) and have the game specific code run it if it wants to. In this case, if you are pressing UP or whatever, you call the step function.
...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here


LinkBack URL
About LinkBacks
Mit Zitat antworten


Hello everyone I am new here and I am glad to be part of this amazing community and I think there...
New to forum