Allegro.cc - Online Community

Allegro.cc Forums » Installation, Setup & Configuration » [A5] How do I use the addons?

Credits go to Matthew Leverton for helping out!
This thread is locked; no one can reply to it. rss feed Print
[A5] How do I use the addons?
SpectreNectar
Member #10,969
May 2009
avatar

Me again.

So I'm experimenting with A5 and would like to draw primitive shapes or display bmp files :-)
For that I need addons, right?

I checked, the test.bmp file IS there, but still I see a yellow square. ALLEGRO_BITMAP *bmp = al_load_bitmap("test.bmp"); fails.

The conclusion:
The manual was right.

Quote:

Note: the core Allegro library does not support any image file formats by default. You must use the allegro_image addon, or register your own format handler

Recently I asked how to link to Allegro5. Today I'm asking how to use addons in Code::Blocks 10.

I tried project -> build options -> linker settings, and added liballegro_image-5.0.0-RC2-mt.a (what are the debug ones for?) Didn't work.
Also tried settings -> compiler and debugger -> linker settings , adding the same file. Didn't work.

Can someone help me? Again.

Here's my code in case I actually already have it working but am missing something:

anim.h

#SelectExpand
1#ifndef ANIM_H_INCLUDED 2#define ANIM_H_INCLUDED 3 4#include <stdio.h> 5#include <allegro5/allegro.h> 6#include <allegro5/allegro_image.h> 7 8class Sprite { 9 10 public: 11 12 Sprite(const char * filename); 13 ~Sprite(); 14 15 ALLEGRO_BITMAP *get(); 16 void set(const char *filename); 17 18 virtual void draw(int x, int y); 19 20 protected: 21 22 ALLEGRO_BITMAP *gfx; 23 int w, h; 24 bool isLoaded; 25 bool isInitialized; 26 27}; 28 29 30class Anim : public Sprite { 31 32 public: 33 34 Anim(const char * filename, int frames, int dirs); 35 36 void draw(int x, int y, int a); 37 38 private: 39 //frame, dir etc. 40 int num_frames; 41 int num_dirs; 42 43}; 44 45 46#endif // ANIM_H_INCLUDED

anim.cpp

#SelectExpand
1#include "anim.h" 2 3 4Sprite::Sprite(const char * filename) { 5 6 isInitialized = false; 7 isLoaded = false; 8 gfx = 0; 9 10 w = h = 0; 11 12 set(filename); 13 14} 15 16Sprite::~Sprite() { 17 18 if(isInitialized) { 19 al_destroy_bitmap(gfx); 20 } 21 22} 23 24void Sprite::set(const char * filename) { 25 26 ALLEGRO_BITMAP *bmp = al_load_bitmap("test.bmp"); 27 28 if(!bmp) { 29 bmp = al_create_bitmap(32,32); 30 if(bmp) { 31 32 ALLEGRO_BITMAP * temp = al_get_target_bitmap(); 33 al_set_target_bitmap(bmp); 34 35 al_clear_to_color(al_map_rgb(255, 255, 0)); 36 37 al_set_target_bitmap(temp); 38 39 gfx = bmp; 40 isInitialized = true; 41 } 42 isLoaded = false; 43 } else { 44 gfx = bmp; 45 isLoaded = true; 46 isInitialized = true; 47 } 48 49 if(isInitialized) { 50 w = al_get_bitmap_width(gfx); 51 h = al_get_bitmap_height(gfx); 52 } 53 54} 55 56ALLEGRO_BITMAP * Sprite::get() { 57 58 return gfx; 59 60} 61 62 63void Sprite::draw(int x, int y) { 64 65 al_draw_bitmap(gfx, x,y, 0); 66 67} 68 69 70 71Anim::Anim(const char * filename, int frames=1, int dirs=8) : Sprite(filename) { 72 num_frames = frames; 73 num_dirs = dirs; 74} 75 76void Anim::draw(int x, int y, int a) { 77 78 al_draw_bitmap_region(gfx, 0,0, w, h, x-w/2,y-h/2, 0); 79 80}

Matthew Leverton
Supreme Loser
January 1999
avatar

SpectreNectar
Member #10,969
May 2009
avatar

I had not.
It wasn't obvious to me that I had to call it either. But now I see.
Thank you once again ;D

jmasterx
Member #11,410
October 2009

  al_init(); //allegro itself
  al_init_image_addon(); //bitmaps, pngs, jpegs etc
  al_init_font_addon(); //for fonts, just bitmap ones
  al_init_primitives_addon(); //rectangles, circles, triangles etc
  al_init_ttf_addon(); // true type fonts (scalable vector fonts)
  al_install_mouse(); //the mouse
  al_install_keyboard(); //the keyboard
  al_install_joystick(); //game pads
  al_install_audio(); //for audio, WAVs,
  al_init_acodec_addon(); //audio codecs like OGG and FLAC

Go to: