Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro - Display

This thread is locked; no one can reply to it. rss feed Print
Allegro - Display
Salwan
Member #7,540
July 2006

Hi everyone,

I'm trying to create a small simple utility library using Allegro to make writing apps easier for me, this is the first time I use Allegro, my problem is in the class that handles displaying the screen, the display wrapper that I wrote works fine with fullscreen mode using hardware triple buffering, but it doesn't display anything when switching to windowed mode using software double buffering, and after sometime the application crashes.
I'm on WinXP/VC2005 and using Allegro 4.2.0

Here's the Display class code:

1class Display
2{
3public:
4 Display()
5 {
6 mHasInitialized = false;
7 mHasTripleBuffer = false;
8 page[0] = NULL;
9 page[1] = NULL;
10 page[2] = NULL;
11 screen_width = 0;
12 screen_height = 0;
13 }
14 
15 ~Display()
16 {
17 }
18 
19 bool init( uint32 width, uint32 height, BITMAP* main_screen )
20 {
21 if( gfx_capabilities & GFX_CAN_TRIPLE_BUFFER )
22 {
23 mHasTripleBuffer = true;
24 }
25 else
26 {
27 if( enable_triple_buffer() == 0 )
28 {
29 mHasTripleBuffer = true;
30 }
31 }
32 if( !mHasInitialized )
33 {
34 if( !is_windowed_mode() && mHasTripleBuffer )
35 {
36 // implement hardware triple buffering
37 page[0] = create_video_bitmap( width, height );
38 page[1] = create_video_bitmap( width, height );
39 page[2] = create_video_bitmap( width, height );
40 if( page[0] == 0 || page[1] == 0 || page[2] == 0 )
41 {
42 allegro_message( "ERROR: Failed creating triple buffering pages." );
43 return false;
44 }
45 active_page = 0;
46 clear_bitmap( page[active_page] );
47 if( show_video_bitmap( page[active_page] ) != 0 )
48 {
49 allegro_message( "ERROR: Failed to flip the test triple buffer." );
50 return false;
51 }
52 mHasInitialized = true;
53 screen_width = width;
54 screen_height = height;
55 }
56 else
57 {
58 // implement software double buffering
59 page[0] = create_bitmap( width, height ); // back buffer
60 page[1] = main_screen; // front buffer
61 page[2] = NULL;
62 if( page[0]==0 || page[1]==0 )
63 {
64 allegro_message( "ERROR: Failed creating double buffering." );
65 return false;
66 }
67 active_page = 1;
68 clear_bitmap( page[0] );
69 clear_bitmap( main_screen );
70 //blit( page[0], page[1], 0, 0, 0, 0, width, height );
71 mHasInitialized = true;
72 screen_width = width;
73 screen_height = height;
74 }
75 }
76 return true;
77 }
78 
79 BITMAP* screen()
80 {
81 if( mHasInitialized )
82 return page[active_page];
83 else
84 return NULL;
85 }
86 
87 bool flip()
88 {
89 if( mHasInitialized )
90 {
91 if( mHasTripleBuffer && !is_windowed_mode() )
92 {
93 while( poll_scroll() )
94 {
95 // wait
96 }
97 if( request_video_bitmap( page[ active_page ] ) )
98 return false;
99 else
100 show_video_bitmap( page[ active_page ] );
101 active_page = (active_page+1)%3;
102 }
103 else
104 {
105 // blit software double buffering
106 blit( page[active_page], page[1], 0, 0, 0, 0, screen_width, screen_height );
107 }
108 }
109 return true;
110 }
111
112 void cleanup()
113 {
114 for( uint8 i = 0; i<3; i++ )
115 {
116 if( page<i> )
117 {
118 destroy_bitmap( page<i> );
119 page<i> = NULL;
120 }
121 }
122 return;
123 }
124 
125protected:
126 bool mHasInitialized;
127 bool mHasTripleBuffer;
128 BITMAP* page[3];
129 uint8 active_page;
130 uint16 screen_width;
131 uint16 screen_height;
132};

Onewing
Member #6,152
August 2005
avatar

Quote:

when switching to windowed mode

Do you mean, while your program is running? Switching modes while running can cause a whole other set of questions/problems.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Salwan
Member #7,540
July 2006

No I did not mean that, it does not takeplace while the app is running, whether fullscreen or windowed is decided by the app before setting a gfx mode.
The class as whole is meant to be very simple, maybe I'll expand it later to include switching the gfx mode in real time, but not before I get the windowed mode running and make sure it's bug-free.

miran
Member #2,407
June 2002

Your application crashes on cleanup if you init it with "screen" as a parameter. You're not supposed to destroy the screen bitmap.

--
sig used to be here

Salwan
Member #7,540
July 2006

Thank you very much for the replies.

miran: You are very correct sir, that's why the app crashes on exit, thank you.

I still don't see anything when in windowed mode, just a blank white background, I'll leave it for now and I'll probably go back to review all the code all over again in a couple of days, maybe then I'll find something I did not notice before?

Trent Gamblin
Member #261
April 2000
avatar

You're setting active_page to 1 in init then blitting page 1 to page 1 in flip. You should set active_page to 0 in init.

Salwan
Member #7,540
July 2006

[quote=Trent Gamblin]
You're setting active_page to 1 in init then blitting page 1 to page 1 in flip. You should set active_page to 0 in init.
</quote>

Oh...My...God... that is so embarrassing.
I don't exactly know how I did not notice that although I wrote the same thing twice, and reviewed the whole code a couple of times.
Shame on me. :-[
I should consult a physician after this.
Sorry for bothering you all.

Thank you Trent Gamblin for the solution, and thank you all who replied, next time I will try much harder to find the problem.

Trent Gamblin
Member #261
April 2000
avatar

It happens to me all the time. :)

Ceagon Xylas
Member #5,495
February 2005
avatar

Don't be hard on yourself, I didn't know how to use a makefile until 2 days ago. =]

Go to: