Class crimes with function yo.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Tvspelsfreak
Chaos Rift Junior
Chaos Rift Junior
Posts: 272
Joined: Wed Sep 29, 2004 5:53 pm
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: C/C++
Location: Umeå, Sweden
Contact:

Post by Tvspelsfreak »

No, cout doesn't work on the dreamcast.

printf("Text");

\t = tab
\n = newline

For variables:
%d = integer
%f = float
%ld = long
%s = string
and so on...

To print a variable, say an int:
int a = 101237;
printf("%d",a);

To print several integers:
int a=1283, b=123746;
printf("%d %d",a,b);

Basically, the variable will be inserted wherever %(something) is.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

He doesn't mean like literally return the value, he means it's doing it through pointers.

By the way, what Tvspelsfreak posted is C-style pointers. In C++ you only need &s in the function prototype and don't have to refer to it as a pointer in the function.

Evidently, that's called "passing a reference". I'm pretty against that method as I learned C first so that really confuses the hell outta. Me. I generally stick with my C-whore * pointer references in the functions. I think that might even be more efficient anyway.

Maybe this can help:

&variable = the adress of the variable in memory
variable = the actual variable
*variable = a pointer to the actual variable through the memory adress.

I'll give you the lowdown on printf (seriously this time). XD

the basic structure is like this:

printf("string here.");
Don't forget your \a \n \ts and crap you can do with cout.

Printing variables is a bit different. First, you need a percent sign '%' then you need a letter corresponding to the corect datatype.

d = int
f = float
lf = double
x = hex

Then, you end the string and put a comma followed by the variables in the correct order that you printed them.

that's pretty much all you need to know. I'll give you some examples:

Code: Select all

printf("Here is a float: %f\nHere is an int: %d", float_variable, int_variable);

printf("Lookie, even hex for keyboard goodness: %x\n", key);

cout << "Run this all j00 C++ whores! << endl;
while(1)
    printf("\a");
There you have it, the almighty C printf. Printf ownz cout at every application in my opinion and looks neater. Also, it is 100^100 times easier to format text with printf.

I personally prefer printf to cout for all of my text printing needs. ^_^
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Class crimes with function yo.

Post by Falco Girgis »

DAMN! I accidently pressed edit instead of quote. :oops:

- Tvspelsfreak
Last edited by Falco Girgis on Tue Jan 25, 2005 10:46 am, edited 2 times in total.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Welp, I gots everything working. Thanks foos for the facts.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Now will you finally quit with all your damn unnecessary dynamic allocation and pure C++ whore-ism?
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

...... NEVER!!!! BWUAHAHHAHAA!!!!
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Class crimes with function yo.

Post by Falco Girgis »

JS Lemming wrote:Yeah, I've been tring to automate some stuff and all and seem to have hit a wall. See, I wanted to create a class that held the width and height of a texture so you woulnd't have to manually enter in the draw function and such. Well, this is what I got:

First the class:

Code: Select all

class Image {
public:
	pvr_ptr_t img;
	int w;
	int h;
};
Now the load image function (the problem):

Code: Select all

Image* LoadImage(const char* file, int width, int height) 
{
	//Create instance
	Image* ImageName = new Image;
	ImageName.img = pvr_mem_malloc(width*height*2);
	ImageName.w = width;
	ImageName.h = height;
	png_to_texture(file, ImageName.img, PNG_FULL_ALPHA);

	return ImageName;
}
And this is how I called the function:

Code: Select all

	Image* p_Room = LoadImage("/rd/room.png",256,256);
What I wanted it to do was create an instance of class image and store the texture, width, and height in there. Well, this is the error it shot me.
Deleting intermediate files and output files for project 'ChainSawR5 - Win32 Debug'.
--------------------Configuration: ChainSawR5 - Win32 Debug--------------------
Microsoft (R) Program Maintenance Utility Version 6.00.8168.0
Copyright (C) Microsoft Corp 1988-1998. All rights reserved.
C:\DevKitDC\bin\createim c:\CHAINSAWRALLY5\DC_TESTS\ChainSawR5\romdisk.img c:\CHAINSAWRALLY5\DC_TESTS\ChainSawR5\romdisk
0 rom 41f08830 [0xffffffff, 0xffffffff] 37777777777, sz 0, at 0x0
1 . [0x1000 , 0x2b44c13e] 0040755, sz 0, at 0x20
1 .. [0x1000 , 0x279a008f] 0040755, sz 0, at 0x40 [link to 0x20 ]
1 room.png [0x19d3 , 0x251059d5] 0100644, sz 14721, at 0x60
C:\DevKitDC\bin\createo c:\CHAINSAWRALLY5\DC_TESTS\ChainSawR5\romdisk.img romdisk c:\CHAINSAWRALLY5\DC_TESTS\ChainSawR5\romdisk.o
C:\DevKitDC\bin\g++ -o ChainSawR5.elf romdisk.o startup.o ChainSawR5.cpp -I C:\DevKitDC\include -I C:\DevKitDC\kernel\arch\dreamcast\include -ml -m4-single-only -O2 -fno-builtin -fno-strict-aliasing -fomit-frame-pointer -fno-optimize-sibling-call
s -nostartfiles -nostdlib -Wl -Ttext=0x8c010000 -L C:\DevKitDC\lib -L C:\DevKitDC\sh-sega-dreamcast\lib -lparallax -lpng -lz -lm -ltremor -lkallisti -lgcc -ltsunami -lk++ -lpcx -Ttext=0x8c010000
In file included from ChainSawR5.cpp:19:
Input.cpp:12:1: warning: no newline at end of file
In file included from ChainSawR5.cpp:20:
Image.cpp: In function `Image* LoadImage(const char*, int, int)':
Image.cpp:14: request for member `img' in `ImageName', which is of
non-aggregate type `Image*'
Image.cpp:15: request for member `w' in `ImageName', which is of non-aggregate
type `Image*'
Image.cpp:16: request for member `h' in `ImageName', which is of non-aggregate
type `Image*'
Image.cpp:17: request for member `img' in `ImageName', which is of
non-aggregate type `Image*'
NMAKE : fatal error U1077: 'C:\DevKitDC\bin\g++.exe' : return code '0x1'
Stop.
Error executing NMAKE.

ChainSawR5.exe - 1 error(s), 1 warning(s)
What seems to be teh problem???
Hah, I was looking at this and check this out:

Code: Select all

Image* LoadImage(const char* file, int width, int height) 
{
	//Create instance
	Image* ImageName = new Image;
	ImageName.img = pvr_mem_malloc(width*height*2);
	ImageName.w = width;
	ImageName.h = height;
	png_to_texture(file, ImageName.img, PNG_FULL_ALPHA);

	return ImageName;
}
Lookie, your return type is a pointer to the object. Only it should be the memory adress of the object. Try changing the prototype to this:

Code: Select all

Image& LoadImage(const char* file, int width, int height) 
There, you're returning the memory address and making the pointer that uses it equal to the returning address of the object.

I still don't recommend this, but I mean may as well get it working...
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Oh damn, that still won't compile. That's a pointer, you can't use the dot operator on a dynamicly created object. Here is t3h fixed function:

Code: Select all

Image& LoadImage(const char* file, int width, int height)
{
   //Create instance
   Image* ImageName = new Image;
   ImageName->img = pvr_mem_malloc(width*height*2);
   ImageName->w = width;
   ImageName->h = height;
   png_to_texture(file, ImageName->img, PNG_FULL_ALPHA);

   return ImageName;
}
Yeah, you can't access member functions with a pointer like that.
You would have to do this:

Code: Select all

(*object).whatever = 10;
But that's just a less convenient way of doing:

Code: Select all

object->whatever = 10;
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

I figured it out a long time ago. And with no crazy class allocations:

Code: Select all

class Image {
public:
   pvr_ptr_t img;
   uint32 w;
   uint32 h;
};

void LoadImage(Image *image, const char* file){
	uint32 width;
	uint32 height;
    png_load_texture(file,&image->img,PNG_FULL_ALPHA,&width,&height);
	image->w = width;
	image->h = height;
}
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Post Reply