PDA

View Full Version : Custom variables in an object of an object.



dazappa
03-03-2010, 01:42 AM
I don't know how else to describe it, so I'll try to make myself clear through a series of code snips.

Exhibit A: My object of a TImageSprite (From Andorra 2D)

TCloud = class(TImageSprite)
private
protected
procedure DoCollision(Sprite:TSprite; var Done:boolean);override;
procedure DoMove(timegap:double);override;
public
xspeed,yspeed: integer;
end;

Notice my xspeed and yspeed integers. The problem is, I don't know how to access them.
If I do the below, my variables are not accessible..

Sprite: TImageSprite;
Sprite := TCloud.Create(engine);
with Sprite do
begin
Image := imgs.Items[1];
x := 0;
y := 0;
z := 1;
//xspeed := 0; // Nope. Doesn't exist.
CollisionTester := ColTest;
end;

I have a very sneaking suspicion this is because I can only have the properties, etc, of a TImageSprite, but still, since I created it as my own object with new variables, I had hoped I'd be able to access them.

- Sighs -
In somewhat related news, I gave up on SDL after I discovered through massive trial and error that I could not load a PNG with alpha, then change that alpha later on to change the opacity.. so I went back to my other friend, Andorra.

User137
03-03-2010, 02:12 AM
Use either

with TCloud(Sprite) do

or

with Sprite as TCloud do

or make your Sprite type TCloud instead of TImageSprite.

Stoney
03-03-2010, 02:28 AM
The problem is that you declared Sprite as TImageSprite but you could change its type to TCloud in order to access new members of the inherited class.

And as for SDL:
If I unterstand you correctly, you have and PNG image with alpha channel and want to create an alpha effect using SDL_SetAlpha? That should be work using something like this (Pseudo-code):

var LoadImage, DrawImage: PSDL_Surface;
...
LoadImage := IMG_Load("path/to/my.png");
...
DrawImage := SDL_DisplayFormat(LoadImage);
// if you use SDL_DisplayFormatAlpha instead of SDL_DisplayFormat, you cannot chage the opacity
SDL_FreeSurface(LoadImage);

// GameLoop
while (not Done) do
begin
SDL_BlitSurface(DrawImage, ...);
end;

SDL_FreeSurface(DrawImage);

Assuming of course you are using pure SDL and not SDL + OpenGL. If you are using the latter, just adjust the alpha value when drawing your quad on the screen with glColor4f.

dazappa
03-03-2010, 04:11 AM
@User137: Perfect! :)

@Stoney: Well, I did that, but it doesn't want to play nice. I basically used GIMP to create a PNG image with an anti aliased font (as I know the font won't be available on every system, and didn't want to waste my time with a font lib + including the font for the menu text). Using SDL_SetAlpha, and SDL_DisplayFormat causes that alpha channel on the PNG image to be lost, and a black background is inserted, so I'd also have to key that out, but there goes my beautiful font anyway. (Now, "It's just a font, it's ok for one thing in the entire game to look ugly", well, there are a number of ideas I have that would rely heavily on the ability to load PNG images and change their opacity properly while keeping the default alpha values from the image, and if I can't do this with SDL, I'm going to go elsewhere ;))

Stoney
03-03-2010, 11:52 AM
I basically used GIMP to create a PNG image with an anti aliased font (as I know the font won't be available on every system, and didn't want to waste my time with a font lib + including the font for the menu text). Using SDL_SetAlpha, and SDL_DisplayFormat causes that alpha channel on the PNG image to be lost, and a black background is inserted, so I'd also have to key that out, but there goes my beautiful font anyway.


That sounds odd, as far as I remember I got that working in a few of my games and I'm also using GIMP to create or edit my images.
I will try to create an example if I find the time. :)

WILL
04-03-2010, 02:19 PM
@Stoney: Well, I did that, but it doesn't want to play nice. I basically used GIMP to create a PNG image with an anti aliased font (as I know the font won't be available on every system, and didn't want to waste my time with a font lib + including the font for the menu text). Using SDL_SetAlpha, and SDL_DisplayFormat causes that alpha channel on the PNG image to be lost, and a black background is inserted, so I'd also have to key that out, but there goes my beautiful font anyway. (Now, "It's just a font, it's ok for one thing in the entire game to look ugly", well, there are a number of ideas I have that would rely heavily on the ability to load PNG images and change their opacity properly while keeping the default alpha values from the image, and if I can't do this with SDL, I'm going to go elsewhere ;))

Well if you want to play with alpha (and even rotation and scaling) in your games then I'd suggest trying OpenGL thats bundled with SDL in JEDI-SDL. If you try to play with alpha alone even you'll end up just getting frustrated because SDL does not use hardware acceleration for these types of effects. Instead you'd be best to switch to using OpenGL as it does this exclusively, plus it also has a great Ortho (2D) mode which you can draw all your graphics. And you'll find that all your graphics will be much faster too. Faster = More sprites and other effects you can do per frame.

And back on topic; this is exactly what I do for all my game objects. Something else I do to easy the Create and Free process is make my own MyObject.Init and MyObject.Free (with override;) procedures so that most of the nitty gritty can be done within these so that I can just focus on my game design and how the objects interact within the main body of code. Less clutter too. ;)

dazappa
04-03-2010, 08:22 PM
Well, rather than getting my hands dirty with openGL directly, I use Andorra. So that's what I've ended up doing, as it's hardware accelerated and does not have problems with setting the opacity of a partially transparent PNG. In the future, I think I will look into using OGL directly for speed, but for my current project that's 320x240, I think Andorra will suffice ;)