Allegro.cc - Online Community
Post Reply

Allegro.cc Forums » Programming Questions » Draw to bitmap failing

rss feed Print
 1   2 
Draw to bitmap failing
Space cpp
Member #16,322
May 2016

tl;dr: setting the display depth size > 0 causes all bitmaps larger than the display to not accept drawing functions.

The project I'm doing right now auto generates a texture atlas, and after adding a few high resolution textures suddenly the atlas is empty.
Loading big bitmaps and using al_clear_to_color are fine, but any drawing function does not work at all.
After hours of testing I found out that the maximum bitmap width(the atlas bitmap) usable without causing the bug equals to my display size.
By randomly changing my code I found that if I remove the line that sets the depth buffer size the error is gone. But it is a 3D game so I really need it.

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

It would help if you could recreate this with a simple test program that we could all try.

It may not behave the same on all systems.

What are your specs, hardware, and OS? And what compiler was used for Allegro? Which version?

Space cpp
Member #16,322
May 2016

Here you go:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_font.h> 3#include <allegro5/allegro_color.h> 4 5 6#define DISPLAY_WIDTH 1024 7#define DISPLAY_HEIGHT 768 8 9int main(int argc, char **argv) 10{ 11 12 if (!al_init() ) return -1; 13 al_init_font_addon(); 14 15 16 // uncomment this for the bug 17 //al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_SUGGEST); 18 19 ALLEGRO_DISPLAY * display = al_create_display(DISPLAY_WIDTH, DISPLAY_HEIGHT); 20 21 ALLEGRO_FONT * font = al_create_builtin_font(); 22 23 24 ALLEGRO_BITMAP * atlas_texture = al_create_bitmap(DISPLAY_WIDTH + 1, DISPLAY_HEIGHT); 25 if (atlas_texture) 26 { 27 28 ALLEGRO_STATE previous_state; 29 al_store_state(&previous_state, ALLEGRO_STATE_TARGET_BITMAP); 30 al_set_target_bitmap(atlas_texture); 31 32 al_clear_to_color( al_map_rgba_f(0,0,0.5f,1.0f) ); 33 al_draw_text(font, al_map_rgb_f(1,1,1), 10, 10, ALLEGRO_ALIGN_LEFT, "Some text"); 34 35 al_restore_state(&previous_state); 36 37 } 38 39 al_clear_to_color( al_map_rgba_f(0,0,0,0) ); 40 al_draw_bitmap(atlas_texture, 0, 0, 0); 41 al_flip_display(); 42 43 al_rest(5); 44 45 al_destroy_font(font); 46 al_destroy_bitmap(atlas_texture); 47 48 return 0; 49 50}

I'm using windows 7 x64, intel core i7-4790, nvidia geforce gtx 970,
mingw i686 gcc 8.1.0, allegro 5.2.5.1

Update:
I did a workaround by creating the atlas as a memory bitmap, drawing, and then converting it to video bitmap.
During the process I found 2 problems:

al_convert_memory_bitmaps() does nothing. Had to explicitly call al_convert_bitmap.

For al_store_state, ALLEGRO_STATE_BITMAP and ALLEGRO_STATE_ALL are not saving the new bitmap flags. So I had to store ALLEGRO_STATE_NEW_BITMAP_PARAMETERS alone and restore it.

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

Well, better that you discovered these bugs than let them remain in the wild. I'm confident we can get a fix out here.

So I modified your example a little bit to do some more in depth testing.

The altered code :

#SelectExpand
1 2#include "allegro5/allegro.h" 3#include "allegro5/allegro_font.h" 4#include "allegro5/allegro_color.h" 5#include "allegro5/allegro_ttf.h" 6 7#include <cstdio> 8 9 10int main(int argc , char** argv) { 11 12 (void)argc; 13 (void)argv; 14 15 int DISPLAY_WIDTH = 1024; 16 int DISPLAY_HEIGHT = 768; 17 18 19 if (!al_init() ) {return -1;} 20 if (!al_init_font_addon()) {return -2;} 21 22 if (!al_install_keyboard()) {return -10;} 23 24 25 printf("Creating %d x %d display...\n" , DISPLAY_WIDTH , DISPLAY_HEIGHT); 26 27 /// uncomment this for the bug 28 al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_SUGGEST); 29 30/// al_set_new_display_flags(ALLEGRO_FULLSCREEN); 31 32 ALLEGRO_DISPLAY * display = al_create_display(DISPLAY_WIDTH, DISPLAY_HEIGHT); 33 if (!display) { 34 printf("Failed to create display.\n"); 35 return 1; 36 } 37 38 39 40 ALLEGRO_FONT * font = al_create_builtin_font(); 41 42 printf("Created display dimensions : %d x %d\n" , al_get_display_width(display) , al_get_display_height(display)); 43 44 int ATLAS_WIDTH = al_get_display_width(display) + 4; 45 int ATLAS_HEIGHT = al_get_display_height(display); 46 47 const int maxsize = al_get_display_option(display , ALLEGRO_MAX_BITMAP_SIZE); 48 49 printf("Max bitmap size is %d x %d\n" , maxsize , maxsize); 50 51 printf("Creating %d x %d atlas bitmap...\n" , ATLAS_WIDTH , ATLAS_HEIGHT); 52 53 ALLEGRO_BITMAP * atlas_texture = al_create_bitmap(ATLAS_WIDTH , ATLAS_HEIGHT); 54 if (!atlas_texture) { 55 printf("Failed to create atlas.\n"); 56 return -20; 57 } 58 59 const int flags = al_get_bitmap_flags(atlas_texture); 60 61 if (flags & ALLEGRO_MEMORY_BITMAP) { 62 printf("Atlas is a memory bitmap!\n"); 63 } 64 65 66 al_set_target_bitmap(atlas_texture); 67 al_clear_to_color(al_map_rgb(255,127,0)); 68 al_draw_text(font , al_map_rgb(0,0,0) , 10 , 10 , ALLEGRO_ALIGN_LEFT , "Hello depth test"); 69 al_set_target_backbuffer(display); 70 71 72 73 ALLEGRO_EVENT_QUEUE* q = al_create_event_queue(); 74 75 al_register_event_source(q , al_get_keyboard_event_source()); 76 al_register_event_source(q , al_get_display_event_source(display)); 77 78 79 80 al_clear_to_color( al_map_rgba_f(0,0,0,0) ); 81 al_draw_bitmap(atlas_texture, 0, 0, 0); 82 al_flip_display(); 83 84 ALLEGRO_EVENT ev; 85 al_wait_for_event(q , &ev); 86 87 al_destroy_font(font); 88 al_destroy_bitmap(atlas_texture); 89 90 return 0; 91 92}

For now I left out testing of state storage and retrieval. That's a separate bug.

I can't reproduce the bug with my code, or with yours.

The one thing I can do is make the text disappear when using an NVIDIA gpu.

I also tested storing the target bitmap and restoring it, and it worked fine in all cases.

Can you provide a static debugging binary I can try that exhibits this problem for you? If it works for me and not you, then there is something different between our hardware that is causing it, or possible the Windows version.

I used Allegro 5.2.5 for all testing.

Space cpp said:

For al_store_state, ALLEGRO_STATE_BITMAP and ALLEGRO_STATE_ALL are not saving the new bitmap flags. So I had to store ALLEGRO_STATE_NEW_BITMAP_PARAMETERS alone and restore it.

I didn't test for this, so this probably is still a bug of some kind.

Space cpp
Member #16,322
May 2016

Haven't installed the static allegro yet, but I did a test in OpenGL mode and it works fine. So I guess it is something on the direct3D implementation.

I also tested storing the target bitmap and restoring it, and it worked fine in all cases.

No no, this part works fine. The problem was just with the new bitmap flags. if I stored ALLEGRO_STATE_BITMAP or ALLEGRO_STATE_ALL then called al_convert_bitmap it was still a memory bitmap rather than a video one.

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

Space cpp said:

if I stored ALLEGRO_STATE_BITMAP or ALLEGRO_STATE_ALL then called al_convert_bitmap it was still a memory bitmap rather than a video one.

This is definitely a bug then. New bitmaps flags should be restored along with the rest of the state at least I think so.

Space cpp
Member #16,322
May 2016

What are the required parameters for the static debug build? I'm using -lallegro_monolith-static and getting lots of undefined reference errors.

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

-lallegro_monolith-debug-static
-ljpeg 
-ldumb 
-lFLAC 
-lfreetype 
-lvorbisfile 
-lvorbis 
-logg 
-lphysfs 
-lpng16 
-lzlibstatic 
-ldsound
-lgdiplus 
-luuid 
-lkernel32 
-lwinmm 
-lpsapi 
-lopengl32 
-lglu32 
-luser32 
-lcomdlg32 
-lgdi32 
-lshell32 
-lole32 
-ladvapi32 
-lws2_32 
-lshlwapi 
-static-libstdc++ 
-static-libgcc

Space cpp
Member #16,322
May 2016

It says I'm missing -lzlibstatic

I downloaded the dependencies here:
https://github.com/liballeg/allegro_winpkg/releases

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

So link to the zlib that came with the winpkg release. Those are the list of libraries when you build allegro and everything else yourself.

Space cpp
Member #16,322
May 2016

Now I'm getting this:

#SelectExpand
1C:\mingw32_gcc810\i686-w64-mingw32\lib\liballegro_monolith-debug-static.a(d3d_display_formats.cpp.obj) In function `al_d3d_generate_display_format_list': 249 C:\dev\allegro_winpkg\universal\allegro\src\win\d3d_display_formats.cpp undefined reference to `__cxa_guard_acquire' 349 C:\dev\allegro_winpkg\universal\allegro\src\win\d3d_display_formats.cpp undefined reference to `__cxa_guard_release' 450 C:\dev\allegro_winpkg\universal\allegro\src\win\d3d_display_formats.cpp undefined reference to `__cxa_guard_acquire' 550 C:\dev\allegro_winpkg\universal\allegro\src\win\d3d_display_formats.cpp undefined reference to `__cxa_guard_release' 649 C:\dev\allegro_winpkg\universal\allegro\src\win\d3d_display_formats.cpp undefined reference to `__cxa_guard_abort' 750 C:\dev\allegro_winpkg\universal\allegro\src\win\d3d_display_formats.cpp undefined reference to `__cxa_guard_abort' 8C:\mingw32_gcc810\i686-w64-mingw32\lib\liballegro_monolith-debug-static.a(d3d_display_formats.cpp.obj) d3d_display_formats.cpp:(.eh_frame+0xab): undefined reference to `__gxx_personality_v0' 9C:\C programing\Testing\bitmapbug\collect2.exe [Error] ld returned 1 exit status

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

You aren't using the same compiler that was used to generate the binaries.

Space cpp
Member #16,322
May 2016

So in order to use allegro-i686-w64-mingw32-gcc-7.4.0-posix-dwarf-static-5.2.5.1 I do need mingw exactly at version 7.4.0?

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

7.4.X will work. You must compile as c++, because the windows d3d code is in c++

Space cpp
Member #16,322
May 2016

Do you know where I can download it? I already searched mingw sourceforge page and google, can't find it.

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

GullRaDriel
Member #3,861
September 2003
avatar

Or also: instructions to install allegro5 with msys2 I made for myself

https://www.allegro.cc/forums/thread/617736/1041205#target

Another one by Polybios:

https://www.allegro.cc/forums/thread/616828

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Space cpp
Member #16,322
May 2016

At first I tried the binaries you provided with a fresh mingw install. Still got the same undefined reference errors.

Now I'm doing the MSYS2 way.
I was able to compile a regular executable with dll dependencies with it, but for a static debug build it seems to be missing the necessary lib files... or am I using incorrect command line parameters? ???

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

I'm guessing you forgot to define ALLEGRO_STATICLINK before including the allegro headers. If you linked to all the libs above, and you're still getting errors, post them. And please post your command line, so we can see what's going on.

Space cpp
Member #16,322
May 2016

Ok, added #define ALLEGRO_STATICLINK

The command line:
gcc main.cpp -Wall -o bitmapbug -lallegro_monolith-debug-static -ljpeg -ldumb -lFLAC -lfreetype -lvorbisfile -lvorbis -logg -lphysfs -lpng16 -ldsound -lgdiplus -luuid -lkernel32 -lwinmm -lpsapi -lopengl32 -lglu32 -luser32 -lcomdlg32 -lgdi32 -lshell32 -lole32 -ladvapi32 -lws2_32 -lshlwapi -static-libstdc++ -static-libgcc

results in:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lallegro_monolith-debug-static
collect2.exe: error: ld returned 1 exit status

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

You're not setting the compiler and linker search directories.

Use -I "path/to/include" and -L "path/to/lib" in your command line. Compiler include search paths go before your source files and linker search paths go after them. Order matters.

Also, you have to use g++, not gcc.

Space cpp
Member #16,322
May 2016

It wasn't that, I'm really lacking the allegro-debug-static lib. I thought msys was going to install it too. Do this means the only way is to build allegro myself to generate it?

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

No, it comes with my binaries. Msys binaries use a different name(s).

Space cpp
Member #16,322
May 2016

Got it compiling finally. However, when I try to run the executable it says something like "The procedure entry point timeBeginPeriod could not be located in the dynamic link library KERNEL32.dll.
Also, the file is 8.11mb, is this normal?

----------
My games

Edgar Reynaldo
Member #8,592
May 2007
avatar

The size is normal for a static debugging executable, yes.

Not sure why timeBeginPeriod is not being found. It's in winmm, and you linked to that, right?

Post Reply
 1   2 


Go to: