Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Help With C++ Objects

This thread is locked; no one can reply to it. rss feed Print
Help With C++ Objects
Genius314
Member #6,626
November 2005

I recently started using C++ OO, and I'm a complete n00b.
I can't quite figure out where the code goes for loading and drawing sprites. Everywhere I put it, it doesn't work. Does it go inside the class, or in with the initializations?

Oh, and also, is there a way to get one tile or sprite from a sprite sheet or tileset? Because right now, i have the sprites in spritesheets, and I'm not sure how to just get a small section of it. Is there some sort of cropping code?

Thanks,
Genius314

miran
Member #2,407
June 2002

Quote:

I can't quite figure out where the code goes for loading and drawing sprites.

Anywhere you want.

Quote:

Everywhere I put it,

Define everywhere.

Quote:

it doesn't work

Define doesn't work.

Quote:

Does it go inside the class, or in with the initializations?

Inside the what?

Quote:

Oh, and also, is there a way to get one tile or sprite from a sprite sheet or tileset? Because right now, i have the sprites in spritesheets, and I'm not sure how to just get a small section of it. Is there some sort of cropping code?

BITMAP *create_sub_bitmap(BITMAP *parent, int x, y, width, height)

--
sig used to be here

kentl
Member #2,905
November 2002

Actually Genius314 you should read a C++ tutorial and complete its assignments before you start making games. I think you will find that it's well invested time, and you've got to learn this stuff anyway if you want to create games using C++.

Jonny Cook
Member #4,055
November 2003

1int main() {
2 allegro_init();
3 install_keyboard();
4 set_color_depth(16);
5 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
6 
7 BITMAP *buffer = creat_bitmap(SCREEN_W, SCREEN_H);
8 
9 // Load stuff
10 BITMAP *sprite = load_bitmap("some.bmp", NULL);
11 
12 bool done = false;
13 while (!done) {
14 // do logic and input
15 if (key[KEY_ESC]) {
16 done = true;
17 }
18 
19 // Draw stuff
20 clear_to_color(buffer, makecol(255, 255, 255));
21 draw_sprite(buffer, sprite, 0, 0);
22 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
23 }
24 
25 // Cleanup
26 destroy_bitmap(buffer);
27 return 0;
28}
29END_OF_MAIN()

Something like that.

The face of a child can say it all, especially the mouth part of the face.

Elverion
Member #6,239
September 2005
avatar

Normally, you don't want to load or toy with bitmaps within an object. It's a bad idea. You should maybe load a datafile full of all your sprites at the beginning of your program as a global datafile, then in your objects constructor do something like

MyObject::MyObject()
{ // MyObject's constructor
  mysprite = (BITMAP *)graphics_dat[MYSPRITE].dat;
}

Then, make your object have a draw function that draws onto the specified bitmap.

void MyObject::draw(BITMAP *dest)
{
  masked_blit(mysprite, dest, 0, 0, x, y, mysprite->w, mysprite->h);
}

Of course, you could get a little bit more fancy and make an array of BITMAP * for use in multiple frames using create_sub_bitmap. I'll leave that up to you to toy around with. Remember to unload your datafile at the end of the program.

--
SolarStrike Software - MicroMacro home - Automation software.

piccolo
Member #3,163
January 2003
avatar

i think you should check out my game it will be a great help to you http://www.allegro.cc/depot/Thegame/

wow
-------------------------------
i am who you are not am i

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

I recently started using C++ OO, and I'm a complete n00b.

Good to hear you are aware of that.

Quote:

I can't quite figure out where the code goes for loading and drawing sprites. Everywhere I put it, it doesn't work.

That's probably because you lack essential coding skills.

Quote:

Does it go inside the class, or in with the initializations?

The class? There can be more than one class, you know. And if by "initialization", you mean "constructor", I'd say rather not. But the question in itself is void, unless you tell us what else it needs to do.

Quote:

Oh, and also, is there a way to get one tile or sprite from a sprite sheet or tileset? Because right now, i have the sprites in spritesheets, and I'm not sure how to just get a small section of it. Is there some sort of cropping code?

blit() and create_sub_bitmap() can help a lot, although they are not fully-fledged miracle tools that do all of your dirty work for you. They can't be, because they don't know how your sprite sheet is laid out. I'm afraid you need to do this yourself.

Anyway, considering your very first sentence, I'd say -
1. Get a decent C++ book. An actual book (one of those paper thingies, you know) is a lot better than any online tutorial. Work through the book, and finish the assignments - or at least be sure that you can, and that you grasp the concepts covered.
2. Code your own "Hello world"-program, using allegro.
3. Guess-the-number, tic-tac-toe, pong, that kind of things - stuff an experienced programmer hacks together in 30 minutes.
4. In case of doubt, RTFM.
5. If still in doubt, come back.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Genius314
Member #6,626
November 2005

Ok, now i feel really stupid.
Apparently the only problem is that i can't name classes as "player." Any other name works.
:-[

LennyLen
Member #5,313
December 2004
avatar

Quote:

Apparently the only problem is that i can't name classes as "player." Any other name works.

try using namespaces.

Tobias Dammers
Member #2,604
August 2002
avatar

Or else, use one of the common style conventions that prevent things like this to happen. The most common one would be this:
"All classes are prefixed with a 'c', and each word in the identifier starts with a capital: class cPlayer { ... };"

A more allegro-ish way would be to use UPPER_CASE_WITH_UNDERSCORES for all type names, macros and constants, and lower_case_with_underscores() for variables and function names.

It's not important which convention you use, as long as you choose one and stick to it. It helps to be able to see what kind of indentifier you're looking at when you see it.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Ceagon Xylas
Member #5,495
February 2005
avatar

Quote:

i think you should check out my game it will be a great help to you

Always.

Quote:

Apparently the only problem is that i can't name classes as "player." Any other name works.

That's weird, I name a class 'player' in just about every single one of my games. :P

Go to: