Alright, I have code posted below which is currently supposed to be drawing an image to a buffer, then drawing the buffer to the screen. My problem is that the buffer only appears as black, any ideas?
EDIT: Okay, it appears that everything drawn to the screen is appearing black, what could be causing this?
1 | //this is so we can use Allegro |
2 | #include <allegro.h> |
3 | |
4 | //our main method |
5 | int main(void) |
6 | { |
7 | //set the colour depth |
8 | set_color_depth(16); |
9 | //this initilises Allegro |
10 | if(allegro_init() != 0) |
11 | { |
12 | return 1; |
13 | } |
14 | |
15 | //here we set up a new window to use |
16 | //the first parameter is the driver we're using |
17 | //the next 2 specify height and width |
18 | //the last 2 are for memory allocation |
19 | if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) |
20 | { |
21 | //should te above fail, we want to try and set it using SAFE mode |
22 | if(set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) |
23 | { |
24 | //and if that fails we want to tell the suer why the program will not work |
25 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
26 | allegro_message("Unable to initiate any graphics mode!\n$s\n", allegro_error); |
27 | return 1; |
28 | } |
29 | } |
30 | |
31 | //sets the colour palette |
32 | set_palette(default_palette); |
33 | |
34 | //clear the screen to be entirely white |
35 | clear_to_color(screen, makecol(255, 255, 255)); |
36 | |
37 | //on windows it is faster if you aquire the screen before drawing to it |
38 | acquire_screen(); |
39 | |
40 | //this installs the keyboard driver so we can use it |
41 | install_keyboard(); |
42 | |
43 | //alright, this is where I'm declaring and creating some bitmaps to use |
44 | BITMAP *ball, *ground, *sky; |
45 | BITMAP *buffer = NULL; |
46 | |
47 | ball = create_bitmap(20, 20); |
48 | ground = create_bitmap(20, 20); |
49 | sky = create_bitmap(20, 20); |
50 | buffer = create_bitmap(640, 480); |
51 | |
52 | ball = load_bmp("ball.bmp", NULL); |
53 | ground = load_bmp("ground.bmp", NULL); |
54 | sky = load_bmp("ground.bmp", NULL); |
55 | |
56 | //these 2 vars are for the ball's X and Y position |
57 | int ball_x, ball_y; |
58 | //giving them default values |
59 | ball_x = 400; |
60 | ball_y = 100; |
61 | |
62 | //drawing everything to the buffer |
63 | //most of the screen will be the sky |
64 | for(int i = 0; i < SCREEN_H - 100; i = i + 20) |
65 | { |
66 | for(int j = 0; j < SCREEN_W - 20; j = j + 20) |
67 | { |
68 | draw_sprite(buffer, sky, i, j); |
69 | blit(buffer, screen, 0, 0, 0, 0, 640, 480); |
70 | } |
71 | } |
72 | |
73 | //you have to release the screen before calling any more functions |
74 | release_screen(); |
75 | |
76 | //now we wait for a key press |
77 | readkey(); |
78 | clear_bitmap(buffer); |
79 | return 0; |
80 | } |
81 | |
82 | //we have to call this when using Allegro |
83 | END_OF_MAIN() |
you don't need to set the color palette in 16b mode.
Also, blit to the screen after you have drawn everything to the buffer.
I'm not sure what's actually causing the problem though, you might want to try taking out acquire and release screen functions.
AH! set the color depth AFTER allegro_init(); that would definately keep it from working like you want.
load_bitmap does its own 'create' your create_bitmaps are causing memory leaks
are the bitmaps your loading 32bit ? or 8 bit ?
Mark: Noticed the drawing the buffer afterwards just after posting.
I saved the buffer as a BMP after everything has been drawn and it's black too.
The images are all 16-Bit.
EDIT: Removing the aquire and release_screen methods didn't work, nor did changing where the set_color_depth method is called, and removing it entirely.
A J: Didn't know aobut that, the tutorial I read said to do it that way, thanks for letting me know.
See if you can draw primitives onto the screen.
Or try saving the loaded images as bmps and see if there is anything in them.
Seems like an odd problem anyway.
They're all entirely black.
Primitives appear.
Are you loading correctly the images? Are they in the right format (8 or 16 bpp)?
Yes, they're loading correctly and are all 16-Bit.
Don't call set_palette, because that is only useful on 8bpp. Also, in the for cycle you have the wrong variable order (i counts until SCREEN_H, thus it is y, and you are using it as x).
I guess other Allegro demo/programs work correctly, right? Can you post the updated code again with all the fixes you have done?
None of that sorted it. Updated code. If anyone wants to download the source along with the images and see if it works for thme, that'd be great.
1 | //this is so we can use Allegro |
2 | #include <allegro.h> |
3 | |
4 | //our main method |
5 | int main(void) |
6 | { |
7 | //this initilises Allegro |
8 | if(allegro_init() != 0) |
9 | { |
10 | return 1; |
11 | } |
12 | |
13 | //here we set up a new window to use |
14 | //the first parameter is the driver we're using |
15 | //the next 2 specify height and width |
16 | //the last 2 are for memory allocation |
17 | if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) |
18 | { |
19 | //should te above fail, we want to try and set it using SAFE mode |
20 | if(set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) |
21 | { |
22 | //and if that fails we want to tell the suer why the program will not work |
23 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
24 | allegro_message("Unable to initiate any graphics mode!\n$s\n", allegro_error); |
25 | return 1; |
26 | } |
27 | } |
28 | |
29 | //set the colour depth |
30 | set_color_depth(16); |
31 | |
32 | //clear the screen to be entirely white |
33 | clear_to_color(screen, makecol(255, 255, 255)); |
34 | |
35 | //on windows it is faster if you aquire the screen before drawing to it |
36 | acquire_screen(); |
37 | |
38 | //this installs the keyboard driver so we can use it |
39 | install_keyboard(); |
40 | |
41 | //alright, this is where I'm declaring and creating some bitmaps to use |
42 | BITMAP *ball, *ground, *sky; |
43 | BITMAP *buffer = NULL; |
44 | |
45 | buffer = create_bitmap(640, 480); |
46 | |
47 | //load bitmap does it's own create_bitmap, so they aren't needed if I'm loading a bitmap |
48 | ball = load_bmp("ball.bmp", NULL); |
49 | ground = load_bmp("ground.bmp", NULL); |
50 | sky = load_bmp("ground.bmp", NULL); |
51 | |
52 | //these 2 vars are for the ball's X and Y position |
53 | int ball_x, ball_y; |
54 | //giving them default values |
55 | ball_x = 0; |
56 | ball_y = 0; |
57 | |
58 | //drawing everything to the buffer |
59 | //most of the screen will be the sky |
60 | //done in this loop |
61 | for(int i = 0; i < SCREEN_H; i = i + 20) |
62 | { |
63 | for(int j = 0; j < SCREEN_W; j = j + 20) |
64 | { |
65 | draw_sprite(buffer, sky, j, i); |
66 | } |
67 | } |
68 | draw_sprite(buffer, ball, ball_x, ball_y); |
69 | blit(buffer, screen, 0, 0, 0, 0, 640, 480); |
70 | save_bitmap("buffer.bmp", buffer, NULL); |
71 | save_bitmap("ball2.bmp", ball, NULL); |
72 | save_bitmap("sky2.bmp", sky, NULL); |
73 | save_bitmap("ground2.bmp", ground, NULL); |
74 | save_bitmap("screen.bmp", screen, NULL); |
75 | |
76 | |
77 | |
78 | //you have to release the screen before calling any more functions |
79 | release_screen(); |
80 | |
81 | //now we wait for a key press |
82 | readkey(); |
83 | clear_bitmap(buffer); |
84 | return 0; |
85 | } |
86 | |
87 | //we have to call this when using Allegro |
88 | END_OF_MAIN() |
Don't do anything in acquire/release_screen pairs except draw to the screen. Never do anything involving timers or non-trivial code, including installing subsystems, as that's a sure fire way to cause a deadlock. While you have the screen acquired, you keep Allegro from processing timers, and thus you won't properly get input updates or anything. Also, you need to set the color depth before you set the video mode (you can't change color depths after you have already set the graphics mode, after all).
what is a 16 bit image ?
explain ?
you need to save your BMP files as either 8 or 24, or 32bit.
there is no 16bit BMP format that allegro supports
check the return values of all your load_bitmap calls
Does setting the colour depth before initializing Allegro even work?
Set the color depth before set_gfx_mode.
//on windows it is faster if you aquire the screen before drawing to it acquire_screen();
This one isn't needed
//you have to release the screen before calling any more functions release_screen();
This one no more.
Please test that your bitmap are correctly loaded by adding if( !sky ) after the loading.
Thanks for all the feedback, it turns out it was because the iamges were 16-Bit Bitmaps, I converted them to 24-Bit and it's working fine now, thanks for letting me know.
All feedback has been taken onboard, thank you.