Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Class not working

This thread is locked; no one can reply to it. rss feed Print
Class not working
Vanneto
Member #8,643
May 2007

So I have this basic class to draw buttons:

Button
{
     public: 
         Button();
         ~Button();
         void draw_button(BITMAP *to, BITMAP *image, int x, int y) const
         {
             blit(image, to, 0,0, x, y, image->w, image->h);
         }
};

And here is the file i use it in:

// Including files here
// main() and allegro init stuff...
    
    DATAFILE *data = load_datafile("mydata.dat");
    Button *b;
    b->draw_button(screen, data[button_sometin].dat, SCREEN_W/2, SCREEN_H/2);

// ...

I get these errors.

error: invalid conversion from `void*' to `BITMAP*'
error:   initializing argument 2 of `void Button::draw_button(BITMAP*, BITMAP*, int, int)'

And while were at it, whats the diffrence between object :: sometin, object . sometin and object -> sometin. Thats all. Just tryin' to figure out the basics. I appreciate all help I get!

Thank you!

In capitalist America bank robs you.

X-G
Member #856
December 2000
avatar

You obviously forgot to cast data[blah].dat to a BITMAP*. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Vanneto
Member #8,643
May 2007

Well.. Yeah now its obvious! :D Thank You! What about the diffrent operators? (:: , -> , .) Whats the diffrence between them?

P.S. Thanks for the quick reply, works fine now! :)

In capitalist America bank robs you.

X-G
Member #856
December 2000
avatar

Uh... they do completely different things.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

LennyLen
Member #5,313
December 2004
avatar

Quote:

What about the diffrent operators? (:: , -> , .) Whats the diffrence between them?

:: is the scope operator. -> is basically a combination of * and .

a->b == (*a).b

Vanneto
Member #8,643
May 2007

I really need to brush up the OOP skills. I suck at this. (too much PHP OOP :P).
I have a method in a class called set_bitmap(). It takes a bitmap as a parameter and assigns it to the member variable. Here is the entire class:

1#ifndef _BUTTON_H_
2#define _BUTTON_H_
3/**
4* Class for drawing button on the screen and using actions from it
5**/
6 
7class Button {
8 public:
9 
10 Button();
11 ~Button();
12 
13 void set_bitmap(BITMAP *button)
14 {
15 button_img = button;
16 }
17 
18 void set_coords(int x, int y)
19 {
20 button_x = x;
21 button_y = y;
22 }
23 
24 void draw_button ()
25 {
26 }
27 
28 private:
29 BITMAP *button_img;
30 int button_x,button_y;
31};
32#endif

Yeah I know its not good. But its a start. I also got this in the main file:

// Do allegro init ...
Button *start_b;

DATAFILE *data;
// Assign a bitmap to it
start_b->set_bitmap((*BITMAP)data[start_button].dat);
//... Destroy BITMAP etc. etc.

It compiles fine, but then when I run it. It crashes. I dont know why. Its the "FILENAME.EXE has encountered an unknown error" problem. Anyone know what to do?

Thanks!

In capitalist America bank robs you.

Johan Halmén
Member #1,550
September 2001

Object Oriented Programming Skills :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

X-G
Member #856
December 2000
avatar

Let's see the actual code for it. Obviously there has to be more to it, since you don't even load data with anything in that snippet; so let's see all of it.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Vanneto
Member #8,643
May 2007

1#include <allegro.h>
2#include <stdlib.h>
3#include "spaceship.h"
4#include "button.h"
5#include "printer.h"
6#include "datafile.h"
7 
8int main(int argc, char *argv[])
9{
10 
11 allegro_init();
12 install_timer();
13 install_mouse();
14 install_keyboard();
15 
16 // Set some constants for colors etc.
17 const int RED = makecol(255, 0, 0);
18 const int GREEN = makecol(0, 255, 0);
19 const int BLUE = makecol(0, 0, 255)
20 
21 set_color_depth(32);
22 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
23 
24 // Constants
25 const int CENTER_W = SCREEN_W/2;
26 const int CENTER_H = SCREEN_H/2;
27 
28 DATAFILE *data = load_datafile("data.dat");
29 if(!data)
30 {
31 print_error("Couldnt load DATAFILE!");
32 }
33 
34 Button *start_b;
35 Button *quit_b;
36 
37 // Initialize the buttons
38 start_b->set_bitmap((BITMAP*)data[start_button].dat);
39 // BUG:start_b->set_coords(CENTER_W, CENTER_H + 50);
40 
41 unload_datafile(data);
42 // TODO: MOVE THIS SECTION TO WHEN THE USER SELECTS SOMETHING
43 return 0;
44}
45END_OF_MAIN();

This is it. The class is in my previous post. I holp it will help!

Thanks!

In capitalist America bank robs you.

LennyLen
Member #5,313
December 2004
avatar

Are you even certain the datafile is being loaded? I see no return values being checked.

edit: Ok, I'm blind.

edit2: where is print_error() defined?

Vanneto
Member #8,643
May 2007

Ups. I only copied the class Button include. Now all the headers are there. Sorry about that. Its defined in printer.h, its basically an textout_ex() function but automatically sets RED color and position to center...

slh.ALL.   propDATE   5-20-2007, 0:10propNAME   start_buttonpropORIG   ,d:\CDEV\bitmap\button_start.bmppropXPOS   -1propXSIZ   -1propYPOS   -1propYSIZ   -1BMP       @                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  propDATE   5-20-2007, 0:09propNAME   cursorpropORIG   &d:\CDEV\bitmap\cursor.bmppropXPOS   -1propXSIZ   -1propYPOS   -1propYSIZ   -1BMP                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          propBACK   npropDITH   npropHNAM   datafilepropNAME   GrabberInfopropPACK   0propRELF   npropSORT   npropTRAN   npropXGRD   16propYGRD   16info        For internal use by the grabber

Hmm.. Maybe heres the problem. When I used the grabber I was getting the bitmaps from bitmap/ folder and I saved the datafile to the main folder ( / ). I think its a bit small... And no data is there. Only the paths to the data is there!

EDIT: I tried this:

start_b->set_bitmap(load_bitmap("button_start.bmp", NULL));

Doesent work either. Dont know why. Its a simple class function. The code is a few posts above!

In capitalist America bank robs you.

X-G
Member #856
December 2000
avatar

unload_datafile(data); <-- How do you expect the bitmap to still be available if you immediately unload the datafile again?

EDIT: Check your return values, too. Make sure you're not getting nulls back somewhere (I bet you are, though).

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Vanneto
Member #8,643
May 2007

I unload it after its been assigned to the class function. Yeah thats possible. Ill check for that. Thanks for the advice!

In capitalist America bank robs you.

LennyLen
Member #5,313
December 2004
avatar

Quote:

start_b->set_bitmap(load_bitmap("button_start.bmp", NULL));

Load the bitmap earlier, so that you can check it is being loaded, then pass it to your function.

X-G
Member #856
December 2000
avatar

Quote:

I unload it after its been assigned to the class function.

Yeah, and what do you think happens to the bitmap when you unload it? That's right, your pointer turns into a dangling pointer and when you try to use it, you crash. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Vanneto
Member #8,643
May 2007

I did that. And now I know the problem is definitely in the .dat file. Now there are some possibilites why it doesent work. a) My grabber.exe is corrupt, b) I dont know how to use it and c) Im an idiot.

Gonna make some new bitmaps and try again! :p

EDIT: I am currently making bitmaps with PS CS2. I draw something. And then Save As... / Bitmap (.bmp) / 16 bit mode / Save. Is this right? Or is there better way?

In capitalist America bank robs you.

X-G
Member #856
December 2000
avatar

That should be fine. I don't want to sound condescending - really - but b) is probably the most likely answer. Though it's been a long time since I used the grabber myself, so I'm probably no better myself. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Vanneto
Member #8,643
May 2007

Sorry about that one. Its definetely the class causing the problem. Lets forget the rest of the code. I have this class:

1#ifndef _BUTTON_H_
2#define _BUTTON_H_
3/**
4* Class for drawing button on the screen and using actions from it
5**/
6 
7class Button {
8 public:
9 Button();
10 ~Button()
11 {
12 button_image = 0;
13 }
14 
15 void set_bitmap(BITMAP *bitmap_b)
16 {
17 button_image = bitmap_b;
18 }
19 void set_coords(int x, int y)
20 {
21 button_x = x;
22 button_y = y;
23 }
24 void draw_button(BITMAP *to) const
25 {
26 blit(button_image, to, 0, 0, button_x, button_y, button_image->w,
27 button_image->h);
28 }
29 
30 private:
31 int button_x,button_y;
32 BITMAP *button_image;
33};
34 
35 
36#endif

Ok, all well and fine. And then in the main file:

    Button *BUTTON_START;
    Button *BUTTON_QUIT;

    BUTTON_START->set_bitmap((BITMAP*)data[start_b].dat);
    BUTTON_START->set_coords(CENTER_W, CENTER_H + 20);

Now this causes the crash. Because if I comment out ..->set_bitmap() and ->set_coords everything is fine. So the problem IS in the class. Now whats wrong. I cant see the problem. When I use set_bitmap the specified bitmap parameter is assigned to the class variable. Nothing to it. But why is it causing a crash? Or is something else causing it... Anyway to solve this? Its the only thing stopping me from going on.

P.S. This is definetely the part of code causing the problem. The rest of the code is located a few posts up. And no, the DATAFILE is not the culprit. I blipped the contents of it on the screen and it worked...

Thank you!

In capitalist America bank robs you.

gnolam
Member #2,030
March 2002
avatar

And where are you instantiating your classes?

In the future, post the smallest complete code that exhibits the problem...

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Vanneto
Member #8,643
May 2007

    Button *BUTTON_START;
    Button *BUTTON_QUIT;

Doesent this initialise the classes?

    Button *BUTTON_START;
    Button *BUTTON_QUIT;

    BUTTON_START->set_bitmap((BITMAP*)data[start_b].dat);
    BUTTON_START->set_coords(CENTER_W, CENTER_H + 20);

This causes the problem. The rest of the code it in earlier posts. I thought classname *objectname initialises the class?

Thanks!

In capitalist America bank robs you.

ReyBrujo
Moderator
January 2001
avatar

No, when you have a pointer, you need to make it point to somewhere. Basically, your code is:

Button *BUTTON_START = 0xDEADBEEF;
Button *BUTTON_QUIT = 0xFEEDF00D;

In other words, unless you point the pointers somewhere, they are pointing to trash, and when you try to execute trash with a BUTTON_START->set_bitmap, if you are lucky your game crashes horribly and closes.

(Edited: Try

Button *BUTTON_START = NULL;
Button *BUTTON_QUIT = NULL;

BUTTON_START = new Button;
BUTTON_QUIT = new Button;

(Edited 2: Oh, PHP? Welcome to memory hell then ;))

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Vanneto
Member #8,643
May 2007

Heh thanks. Im already getting headaches from having to deallocating stuff etc. :S
When I try this I get the following when I try to compile:

D:\CDEV\space_invade\main.o:main.cpp:(.text+0x430): undefined reference to `Button::Button()'
D:\CDEV\space_invade\main.o:main.cpp:(.text+0x489): undefined reference to `Button::Button()'

And it doesent compile. Probably my fault. Just checking.

Thanks!

In capitalist America bank robs you.

ReyBrujo
Moderator
January 2001
avatar

You haven't defined the constructor and destructor in your class. Temporarily, replace Button(); in your class with Button() { } and ~Button(); with ~Button() { } (or just delete them altogether), and it should compile. Note that your constructor should set the bitmap member to NULL, and the destrutor, destroy it, and you should call delete BUTTON_START before finishing Allegro, but for now that should work.

If you get a bunch of undefined Allegro stuff, you forgot to add a reference to Allegro in the linking section of the IDE.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Vanneto
Member #8,643
May 2007

I added the -lalleg already. And BTW: IT WORKS!!! Thanks. Something so simple (well, at least now) and changes so much. Thank you very much. This language is causing me so much agony at the moment.. Thanks for all the support from all the people who replied to this thread and helped! And especially thank you ReyBrujo! :D

THANKS!

In capitalist America bank robs you.

gnolam
Member #2,030
March 2002
avatar

Now that the problem is resolved, I must add the following comment:

Quote:

Class not working

I guess it's bourgeoisie then.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Go to: