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

User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Class crimes with function yo.

Post by JS Lemming »

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???
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 »

:watiff: Dude, either I haven't read as much of my C++ book as you have, or this whole thing is totally illegal:

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;
}
From what I see, you're making an instance of Image that is a pointer and a function. Huh what? Okay, maybe I can't answer this.

What I think is that it's totally illegal and stuff.
I might not be C++ whore enough to even know exactly what it is that you're trying to do.

EDIT - I see exactly what you're trying to do, but the compiler is thinking the function is actually being defined as a pointer to an instance of that class.
Last edited by Falco Girgis on Fri Jan 21, 2005 8:55 am, edited 1 time 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 »

This is driving me CRAZY!!! I now have it where the loading image function deal comiles:

Code: Select all

//Define some classes and such
class Image {
public:
	pvr_ptr_t img;
	int w;
	int h;
};

//Loads an image and creates an instance of class Image
Image* LoadImage(const char* file, int width, int height) 
{
	//Create instance
	Image* ClassPointer = new Image;
	ClassPointer->img = pvr_mem_malloc(width*height*2);
	ClassPointer->w = width;
	ClassPointer->h = height;
	png_to_texture(file, ClassPointer->img, PNG_FULL_ALPHA);

	return ClassPointer;
}
But now I'm tring to draw the mug. By sending the Image class pointer to the function so I can access the w and h like so:

Code: Select all

void DrawImage(Image* PicData ,int x, int y, int width, int height, int z, int a) { 
    pvr_poly_cxt_t cxt; 
    pvr_poly_hdr_t hdr; 
    pvr_vertex_t vert; 
    pvr_poly_cxt_txr(&cxt,PVR_LIST_TR_POLY,PVR_TXRFMT_ARGB4444,PicData->w,PicData->h,PicData->img,PVR_FILTER_NONE); 
    pvr_poly_compile(&hdr,&cxt); 
    pvr_prim(&hdr,sizeof(hdr)); 
    vert.argb = PVR_PACK_COLOR(a,1,1,1);    
    vert.oargb = 0; 
    vert.flags = PVR_CMD_VERTEX; 
    vert.x = x;		// - (w/2) - scalex; 
    vert.y = y;		// - (h/2) - scaley;
    vert.z = z; 
    vert.u = 0.0; 
    vert.v = 0.0; 
    pvr_prim(&vert,sizeof(vert)); 
    vert.x = x + width;		// + (w/2) + scalex; 
    vert.y = y;		// - (h/2) - scaley; 
    vert.z = z; 
    vert.u = 1.0; 
    vert.v = 0.0; 
    pvr_prim(&vert,sizeof(vert)); 
    vert.x = x;		// - (w/2) - scalex; 
    vert.y = y + height;		// + (h/2) + scaley; 
    vert.z = z; 
    vert.u = 0.0; 
    vert.v = 1.0; 
    pvr_prim(&vert,sizeof(vert)); 
    vert.x = x + width;		// + (w/2) + scalex; 
    vert.y = y + height;		// + (h/2) + scaley; 
    vert.z = z; 
    vert.u = 1.0; 
    vert.v = 1.0; 
    vert.flags = PVR_CMD_VERTEX_EOL; 
    pvr_prim(&vert, sizeof(vert)); 
}
Now all that actually compiles! BUT, when I run the program, the image is not displayed when I tell it to:

Code: Select all

DrawImage(p_Room,0,0,640,480,8,1);
This is what the transfer box says:

Code: Select all

*** ASSERTION FAILURE ***
Assertion "0" failed at pvr_prim.c:80 in `pvr_poly_compile': Invalid texture U s
ize

arch: calling atexit functions
arch: shutting down kernel
maple: final stats -- device count = 2, vbl_cntr = 47, dma_cntr = 35
vid_set_mode: 640x480IL NTSC

My guess is that the "PicData->w,PicData->h,PicData->img" in the drawimage function is not actually pointing to those things. But why not!?!?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
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 »

Code: Select all

//Loads an image and creates an instance of class Image
Image* LoadImage(const char* file, int width, int height)
{
   //Create instance
   Image* ClassPointer = new Image;
   ClassPointer->img = pvr_mem_malloc(width*height*2);
   ClassPointer->w = width;
   ClassPointer->h = height;
   png_to_texture(file, ClassPointer->img, PNG_FULL_ALPHA);

   return ClassPointer;
}
That function is wierd.
First of all, it's one image.... Why make it dynamic?
Second, you never free the allocated memory....

Here's what I would do:

Code: Select all

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

void LoadImage(Image *image, const char* file){
    png_load_texture(file,&image->img,PNG_FULL_ALPHA,&image->w,&image->h);
}
It compiled just fine and should work fine as well.
If it doesn't, use printf to track down the error.
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 »

WHOA! I tried that and this is what happened. Er.. this is what the transfer window said:
pvr: enabling vertical scaling for non-VGA
kernel panic: out of memory; about to run over kernel stack
arch: aborting the system
Is that common???
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
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 »

Where in the program does it happen?
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 »

Post your exact code.
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 »

Aight.

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){
	image->w = width;
	image->h = height;
    png_load_texture(file,&image->img,PNG_FULL_ALPHA,&image->w,&image->h);
}



//take out Z later!!!
void DrawImage(Image *PicData, int x, int y, uint32 width, uint32 height, int z, int a) { 
    pvr_poly_cxt_t cxt; 
    pvr_poly_hdr_t hdr; 
    pvr_vertex_t vert; 
    pvr_poly_cxt_txr(&cxt,PVR_LIST_TR_POLY,PVR_TXRFMT_ARGB4444,PicData->w,PicData->h,PicData->img,PVR_FILTER_NONE); 
    pvr_poly_compile(&hdr,&cxt); 
    pvr_prim(&hdr,sizeof(hdr)); 
    vert.argb = PVR_PACK_COLOR(a,1,1,1);    
    vert.oargb = 0; 
    vert.flags = PVR_CMD_VERTEX; 
    vert.x = x;		// - (w/2) - scalex; 
    vert.y = y;		// - (h/2) - scaley;
    vert.z = z; 
    vert.u = 0.0; 
    vert.v = 0.0; 
    pvr_prim(&vert,sizeof(vert)); 
    vert.x = x + width;		// + (w/2) + scalex; 
    vert.y = y;		// - (h/2) - scaley; 
    vert.z = z; 
    vert.u = 1.0; 
    vert.v = 0.0; 
    pvr_prim(&vert,sizeof(vert)); 
    vert.x = x;		// - (w/2) - scalex; 
    vert.y = y + height;		// + (h/2) + scaley; 
    vert.z = z; 
    vert.u = 0.0; 
    vert.v = 1.0; 
    pvr_prim(&vert,sizeof(vert)); 
    vert.x = x + width;		// + (w/2) + scalex; 
    vert.y = y + height;		// + (h/2) + scaley; 
    vert.z = z; 
    vert.u = 1.0; 
    vert.v = 1.0; 
    vert.flags = PVR_CMD_VERTEX_EOL; 
    pvr_prim(&vert, sizeof(vert)); 
}

Then I make a global class instance in a header file:

Code: Select all

Image* p_Room;
Then in the main() function. I use the load image funtion:

Code: Select all

LoadImage(p_Room,"/rd/room.png",256,256);
And of course I draw it in the Drawframe thingy:

Code: Select all

DrawImage(p_Room,0,0,640,480,8,1);
It seems like it should work!
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
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 »

png_load_texture reurns the width and height, no need to pass them to the function nor setting them before.
And also, try tracking down where the error occurs by using printf.
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 »

Tvspelsfreak wrote:png_load_texture reurns the width and height, no need to pass them to the function nor setting them before.
And also, try tracking down where the error occurs by using printf.
hrrmm. How can png_load_texture return the width AND height?!?!? Can you give me how my function would look with that? And I have no clue how to use printf()... I'm the anti-c.
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 »

Your anti-C whore-ism is only obvious.

I'll give you the lowdown on printf.

First, we must initialize it.

Code: Select all

pvr_ptr_t printf;
new printf(sizeof(vert));
printf->x = sin(PI*img_width);
Now, to use it:

Code: Select all

printf.layers(buffer) = mem_malloc(uint8 str*);
printf.txt = display("hello world, here is my variable:%d", p_k_m_n_g_var);
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 »

I dunno how else to explain it.
Here's the function prototype:

Code: Select all

/* Load a PNG file, allocating a texture, and returning the size of the file */ 
	int png_load_texture(const char *filename, pvr_ptr_t *tex, uint32 alpha, uint32 *w, uint32 *h);
And:

Code: Select all

Image* p_Room;
Why did you make it a pointer?


I'd suggest:

Code: Select all

Image p_Room;
In main():

Code: Select all

LoadImage(&p_Room,"/rd/room.png",256,256);
And to draw:

Code: Select all

DrawImage(&p_Room,0,0,640,480,8,1);

And you'd better lay off being anti-c if you're gonna code for Dreamcast.
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 »

GyroVorbis wrote:Your anti-C whore-ism is only obvious.

I'll give you the lowdown on printf.

First, we must initialize it.

Code: Select all

pvr_ptr_t printf;
new printf(sizeof(vert));
printf->x = sin(PI*img_width);
Now, to use it:

Code: Select all

printf.layers(buffer) = mem_malloc(uint8 str*);
printf.txt = display("hello world, here is my variable:%d", p_k_m_n_g_var);
Heh heh... PI! Yeah right! Well, tell me really now. I need to know for debugin.


In all my life of programing... never has it been legal or possible to RETURN 2 values... you can't be serious about it returning 2 things!

int Impossible() {
return 5, 18;
}

I think not!!!

And the only reason i made that thing a pointer was cause I just thought you had too. I'm no genious in that category.

I have a big problem realizing when to use p_Room and &p_Room.

The only reason I said I'm anti-c is cause I know nothing about it (I've only learned c++ through random books.) Why is c so great besides printf and speed issues?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
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 »

I didn't mean return in that sense.
I meant like this:

Code: Select all

#include <stdio.h>

void blah(int *a, int *b){
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

void main(){
    int n1 = 10;
	int n2 = 20;

	printf("Before:\t%d,%d\n",n1,n2);

	blah(&n1,&n2);

	printf("After:\t%d,%d\n",n1,n2);
}
Changing the variables by altering what's at the adress instead of sending the variable itself to the function.
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 »

oh i get ya. But man... printf looks really nasty.. %d... %t.. where did that come from. Can I just use cout?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Post Reply