|
Black Sprites |
therpgmaker
Member #13,412
August 2011
|
Ok, so I was working on a map editor, and had it nearing completion when I decided to make the switch from allegro 4 to allegro 5. After much hardship, I finally got it to compile, and it mostly works, except for some major problems. Some of the sprites are black. I am at the moment completely baffled, because I made two scroll bar objects, one vertical, and one horizontal. On these scroll bars, I have a button object with a sprite attached to it for both arrow buttons. The sprite is simply something I created through code. Now the vertical scroll bar looks fine, but the horizontal scroll bar, the right arrow button is black except for the border. No little triangle sprite that coded it to have. The weird part is that the code for the creation of all four directional buttons is virtually identical except for the positions and the relative coords of the triangle on the button. I tried to look for any differences in the code, but to no avail. The only thing that I figured out is that if I reverse the order that I create the left and right buttons, it is the left button that becomes all black rather than the right one. As well the text on the buttons appears as all black, and same with the tiles on the map window. Here's the horizontal scroll bar code for the creation of the left and right arrow buttons: 1//Create left button
2 ALLEGRO_BITMAP *leftArrow = al_create_bitmap(BUTTONWIDTH,height);
3 al_set_target_bitmap(leftArrow);
4 al_draw_filled_rectangle(0,0,BUTTONWIDTH,height,al_map_rgb(255,0,255));
5 al_draw_filled_triangle(5*BUTTONWIDTH/8,height/4,5*BUTTONWIDTH/8,3*height/4,3*BUTTONWIDTH/8,height/2,borderCol);
6 leftButton = new PushButton(x,y,BUTTONWIDTH,height,leftArrow);
7 leftButton->setBorderCol(borderCol);
8 leftButton->setHorizontal(false);
9
10 //Create right button
11 ALLEGRO_BITMAP *rightArrow = al_create_bitmap(BUTTONWIDTH,height);
12 al_set_target_bitmap(rightArrow);
13 al_draw_filled_rectangle(0,0,BUTTONWIDTH,height,al_map_rgb(255,0,255));
14 al_draw_filled_triangle(3*BUTTONWIDTH/8,height/4,3*BUTTONWIDTH/8,3*height/4,5*BUTTONWIDTH/8,height/2,borderCol);
15 rightButton = new PushButton(x+width-BUTTONWIDTH,y,BUTTONWIDTH,height,rightArrow);
16 rightButton->setBorderCol(borderCol);
17 rightButton->setHorizontal(false);
And here is the code for drawing the button. There's a lot of stuff in the middle for highlighting/darkening the button depending whether the mouse is hovering over it or it is being clicked. That works just fine and the problem still occurs even if it is commented out. 1if(visible)
2 {
3 ALLEGRO_COLOR newForeCol = foreCol;
4 ALLEGRO_COLOR newBackCol = backCol;
5 ALLEGRO_COLOR shadowCol = decrementColour(backCol,SHADOWR,SHADOWG,SHADOWB);//Find darker colour on bottom half of button
6 ALLEGRO_COLOR newBorderCol = borderCol;
7
8 ALLEGRO_BITMAP *newSprite;
9
10 if(sprite != NULL)
11 {
12 newSprite = al_create_bitmap(al_get_bitmap_width(sprite),al_get_bitmap_height(sprite));
13 //rectfill(newSprite,x,y,newSprite->w,newSprite->h,makecol(255,0,255));
14 al_set_target_bitmap(newSprite);
15 al_draw_bitmap(sprite,0,0,0);
16 //al_draw_bitmap_region(sprite,0,0,al_get_bitmap_width(sprite),al_get_bitmap_height(sprite),0,0,0);
17 al_convert_mask_to_alpha(newSprite,al_map_rgb(255,0,255));
18 }
19
20 //Hilight button if mouse is over
21 if(highlighted)
22 {
23 newBackCol = incrementColour(newBackCol,0,0,HIGHLIGHT);
24 shadowCol = incrementColour(shadowCol,0,0,HIGHLIGHT);
25 newForeCol = incrementColour(newForeCol,0,0,HIGHLIGHT);
26 newBorderCol = incrementColour(newBorderCol,0,0,HIGHLIGHT);
27 if(sprite != NULL)
28 incrementColour(newSprite,0,0,HIGHLIGHT);
29 }
30
31 //Darken button if pressed
32 if(pressed)
33 {
34 newBackCol = decrementColour(newBackCol,PRESSSHADOWR,PRESSSHADOWG,PRESSSHADOWB);
35 shadowCol = decrementColour(shadowCol,PRESSSHADOWR,PRESSSHADOWG,PRESSSHADOWB);
36 newForeCol = decrementColour(newForeCol,PRESSSHADOWR,PRESSSHADOWG,PRESSSHADOWB);
37 newBorderCol = decrementColour(newBorderCol,PRESSSHADOWR,PRESSSHADOWG,PRESSSHADOWB);
38 if(sprite != NULL)
39 decrementColour(newSprite,PRESSSHADOWR,PRESSSHADOWG,PRESSSHADOWB);
40 }
41
42 al_set_target_bitmap(buffer);
43 al_draw_filled_rectangle(x+1, y+1, x+width, y+height-1, newBackCol); //Draw the button
44 if(horizontal)
45 al_draw_filled_rectangle(x+2, y+1+height/2, x+width-2, y+height-2, shadowCol); //Draw the button darker part on bottom half
46 else
47 al_draw_filled_rectangle(x+1+width/2, y+2, x+width-2, y+height-2, shadowCol); //Draw the button darker part on right half
48
49 if(sprite != NULL)
50 al_draw_bitmap(newSprite, x+width/2-al_get_bitmap_width(sprite)/2, y+height/2-al_get_bitmap_height(sprite)/2,0);
51 //draw_lit_sprite(buffer,sprite, x+width/2-sprite->w/2, y+height/2-sprite->h/2, 64); //Highlight sprite
52
53 //Draw the border
54 al_draw_line(x+1, y, x+width-1, y, borderCol,0); //Top
55 al_draw_line(x+1, y+height, x+width-1, y+height, borderCol,0); //Bottom
56 al_draw_line(x, y+height-1, x, y+1, borderCol,0); //Left
57 al_draw_line(x+width, y+height-1, x+width, y+1, borderCol,0); //Right
58 if(pressed) //Thicken border if pressed
59 {
60 ALLEGRO_COLOR lightBorder = incrementColour(borderCol,BORDERINCR,BORDERINCG,BORDERINCB);
61
62 al_draw_line(x+1, y+1, x+width-1, y+1, lightBorder,0); //Top
63 al_draw_line(x+1, y+height-1, x+width-1, y+height-1, lightBorder,0); //Bottom
64 al_draw_line(x+1, y+height-1, x+1, y+1, lightBorder,0); //Left
65 al_draw_line(x+width-1, y+height-1, x+width-1, y+1, lightBorder,0); //Right
66 }
67
68 //Draw text
69 al_draw_text(myFont,newForeCol,x+width/2,y+height/2-al_get_font_line_height(myFont)/2,ALLEGRO_ALIGN_CENTRE,caption.c_str());
70
71 if(sprite != NULL)
72 al_destroy_bitmap(newSprite);
73 }
Also, I attached a screenshot. |
David Couzelis
Member #10,079
August 2008
|
Were you able to figure out what was wrong? I'm interested in this problem and would like to look in to it. Could you provide either all or a piece of the source code that's able to compile so I could try it out? Just to confirm, there were no "black sprites" when you were using Allegro 4? |
therpgmaker
Member #13,412
August 2011
|
Yeah I figured it out. It ended up just being stupid mistakes I made from not being used to Allegro 5. Some of it (the arrow buttons and tiles) were from not setting the target bitmap (I liked the old way better in this case...) in the main function and then calling the clear to colour function. The text on the other buttons was because I forgot to change which font I was loading, and was instead loading an allegro 4 font. I figured that out by messing with the colour the draw text function was passed and realized that I could make the text show up with a pink background if I set it to white. So yeah, all my own stupid mistakes. EDIT: One question. Am I correct in assuming that allegro 5 coords start at (1,1) instead of (0,0)? |
David Couzelis
Member #10,079
August 2008
|
I switched to Allegro 5 only a couple of weeks ago, but from what I can tell everything is still (0, 0) based. Are you experiencing something different? |
Elias
Member #358
May 2000
|
Coords start at 0/0 - but Allegro 5 uses continuous instead of the A4 pixel coordinates. So 0/0 is not the pixel in the upper left corner of the screen, but the position of the upper left corner of that pixel. Position 0.5/0.5 is the center of that pixel. Position 1/1 is on the intersection of 4 different pixels. -- |
therpgmaker
Member #13,412
August 2011
|
Ok, but then why is it that when I create a bitmap, and then draw a rectangle to it for a border from (0.5,0.5) to (width-.5,height-.5), the border doesn't show up at all on the top or left? |
Trent Gamblin
Member #261
April 2000
|
Is it a memory bitmap? OpenGL or D3D? Which platform? Can you give a code example?
|
therpgmaker
Member #13,412
August 2011
|
ALLEGRO_BITMAP* mapWindow = al_create_bitmap(width,height); al_set_target_bitmap(mapWindow); al_draw_filled_rectangle(.5, .5, width-.5, height-.5, backCol); EDIT: And that might be the problem... that's not me drawing the border, |
Trent Gamblin
Member #261
April 2000
|
The 0.5 offsets are for outlined shapes only. For filled shapes, don't use 0.5 offsets. To understand why, read up on it in the manual, it explains it pretty clearly. I understand you just made a simple mistake, though.
|
Elias
Member #358
May 2000
|
Yes, what Trent says - if you draw a bitmap you usually want it to spawn the complete pixels so you don't want an offset of half a pixel. -- |
therpgmaker
Member #13,412
August 2011
|
If (x+.5,y+.5) is the centre of the pixel, if I make a rounded square border, it should look like this: oxxxo Rather, it looks more like: oxxoo With the following code: al_draw_line(x+1.5, y+.5, x+width-1.5, y+.5, borderCol,1); //Top al_draw_line(x+1.5, y+height-.5, x+width-1.5, y+height-.5, borderCol,1); //Bottom al_draw_line(x+.5, y+height-1.5, x+.5, y+1.5, borderCol,1); //Left al_draw_line(x+width-.5, y+height-1.5, x+width-.5, y+1.5, borderCol,1); //Right I could easily make it look right, but it doesn't this seem consistent at all. EDIT: Wow, those diagrams were hard to format correctly... |
Elias
Member #358
May 2000
|
Look at the pictures here, especially the two al_draw_line calls in the second one: http://www.allegro.cc/manual/5/primitives.html -- |
|