Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Did I skip something?

Credits go to Neil Roy for helping out!
This thread is locked; no one can reply to it. rss feed Print
Did I skip something?
Bob Keane
Member #7,342
June 2006

I am trying to write a game and having an odd problem. I followed the tutorials up to the events and added my own code. Here it is:

#SelectExpand
1#include "allegro5/allegro.h" 2#include "allegro5/allegro_image.h" 3#include "allegro5/allegro_primitives.h" 4#include <stdio.h> 5#include <allegro5/allegro_font.h> 6#include <allegro5/allegro_ttf.h> 7#define white al_map_rgb(255,255,255) 8#define black al_map_rgb(0,0,0) 9#define line_size 2 10 11void print_board(char board, float height, float width, float gr_width, ALLEGRO_FONT * script); 12 13int main(int argc, char **argv){ 14 15 ALLEGRO_DISPLAY *display = NULL; 16 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 17 float display_height = 640; 18 float display_width = 480; 19 20 if(!al_init()) { 21 fprintf(stderr, "failed to initialize allegro!\n"); 22 return -1; 23 } 24 25 display = al_create_display(display_height, display_width); 26 if(!display) { 27 fprintf(stderr, "failed to create display!\n"); 28 return -1; 29 } 30 31 event_queue = al_create_event_queue(); 32 if(!event_queue) { 33 fprintf(stderr, "failed to create event_queue!\n"); 34 al_destroy_display(display); 35 return -1; 36 } 37 38 al_init_image_addon(); 39 al_init_primitives_addon(); 40 al_init_font_addon(); // initialize the font addon 41 al_init_ttf_addon();// initialize the ttf (True Type Font) addon 42 43 ALLEGRO_FONT *font = al_create_builtin_font(); 44 45 if (!font){ 46 fprintf(stderr, "Could not create font.\n"); 47 return -1; 48 } 49 50 float grid_height = display_height - 21; 51 float row_height = grid_height / 24; 52 float grid_width = display_width - 10; 53 float column_width = grid_width / 11; 54 55 al_flip_display(); 56 57 al_register_event_source(event_queue, al_get_display_event_source(display)); 58 59 al_clear_to_color(black); 60 61 al_flip_display(); 62 63 while(1) 64 { 65 ALLEGRO_EVENT ev; 66 ALLEGRO_TIMEOUT timeout; 67 al_init_timeout(&timeout, 0.06); 68 69 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); 70 71 if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 72 break; 73 } 74 75 al_clear_to_color(black); 76 print_board('p', row_height, column_width, grid_width, font); 77 print_board('s', row_height, column_width, grid_width, font); 78 al_draw_line(0,0,display_width,0, white, line_size); 79 al_draw_line(0, 0, 0, display_height, white, line_size); 80 al_flip_display(); 81 } 82 al_destroy_display(display); 83 al_destroy_event_queue(event_queue); 84 85 return 0; 86} 87 88void print_board(char board, float height, float width, float gr_width, ALLEGRO_FONT * script){ 89 90 float x_position = 0; 91 float y_position; 92 if(board == 'p') 93 y_position = 0; 94 else 95 y_position = 12 * height; 96 float temp; 97 for(int count = 0; count <= 11; count++){ 98 al_draw_line(x_position, y_position, gr_width, y_position, white, line_size); 99 y_position = y_position + height;} 100 101 temp = y_position - height; 102 103 if(board == 'p') 104 y_position = 0; 105 else 106 y_position = 12 * height; 107 for(int count = 0; count <= 11; count++){ 108 al_draw_line(x_position, y_position, x_position, temp, white, line_size); 109 x_position = x_position + width;} 110 111 if(board == 'p') 112 y_position = height / 2; 113 else 114 y_position = 12 * height + (height / 2); 115 x_position = width / 2 + width; 116 for(char letter = 'A'; letter <'K'; letter++){ 117 al_draw_textf(script, white, x_position, y_position, ALLEGRO_ALIGN_CENTRE, "%c", letter); 118 x_position = x_position + width;} 119 120 x_position = width / 2; 121 if(board == 'p') 122 y_position = 0; 123 else 124 y_position = 12 * height; 125 y_position = y_position + height + (height / 2); 126 127 for(int count = 1; count < 11; count++){ 128 al_draw_textf(script, white, x_position, y_position, ALLEGRO_ALIGN_CENTRE, "%i", count); 129 y_position = y_position + height;} 130 131 132return; 133}

When I run the program, the grids go roughly 3/4 across the screen and the second is only drawn half. Here is a screen shot.

{"name":"611964","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/a\/8ace26d230bf6346e28449d0e01f768c.png","w":1366,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/a\/8ace26d230bf6346e28449d0e01f768c"}611964

I did try it in full screen and it worked perfectly. I also tried a simple line drawing program, one across the top of the screen and one down the left side. The side line appeared to be correct but the top one was short. Did I get ahead of myself or is something wrong with my program?

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

Neil Roy
Member #2,229
April 2002
avatar

You have your height and width reversed (if you want 640x480).

You have display = al_create_display(display_height, display_width); The first parameter for al_create_display() is the width, the second is the height, you have them reverse, but then you have the wrong values in those variables.

So the width it is calculated on is 480, when you set it to 640 = 3/4.

---
“I love you too.” - last words of Wanda Roy

Bob Keane
Member #7,342
June 2006

That was it. Thanks, but it looks like there is a gap at the bottom of the screen. I guess I'll have to play with my calculations. ... Or is it the frame?

{"name":"611966","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/3\/83857332562bad1aca20e67a993164d7.png","w":1366,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/3\/83857332562bad1aca20e67a993164d7"}611966

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

Neil Roy
Member #2,229
April 2002
avatar

Just a quick look at your height values...

grid_height = 640-21 = 619
row_height = grid_height / 24 = 25.7916

And you draw your line 12 times I think for each grid
12 * 25.7916 = 309.4992

So the last line on the top is at 309.4992

Then you start at 309.4992 and move down that many more which leads to around 618.9984

640 - 618 = ~22 lines space at the bottom. Which looks about right from your screenshot.

Instead of starting at zero for the top grid, how about starting at 11 or so, this way your space at the top will match the bottom. Or don't subtract 21 on your grid height at the start.

---
“I love you too.” - last words of Wanda Roy

Bob Keane
Member #7,342
June 2006

NiteHackr said:

Or don't subtract 21 on your grid height at the start.

That calculation was to allow for the line thickness when computing the cell sizes. I changed it and it looks better, but there is still a gap. I changed the line allowing for line thickness when calculating the cell width and the last one also comes up short. I guess I'll have to work with it. Thanks anyway.

By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

Neil Roy
Member #2,229
April 2002
avatar

Anytime.

It's experience anyhow. Good luck with it.

---
“I love you too.” - last words of Wanda Roy

Go to: