|
al_draw_soft_line |
Francis Langlais
Member #15,985
June 2015
|
I'm trying to use al_draw_soft_line but I can't manage to understand how to use it. So just for a test I tried to just draw a line at every pixel of the line. I expect to have a rectangle ate the end. So I did this: 1void drawline(uintptr_t state, int x, int y);
2
3int main(void) {
4
5 al_init();
6 al_install_keyboard();
7 al_install_audio();
8 al_init_acodec_addon();
9 al_init_image_addon();
10 al_init_primitives_addon();
11
12 ALLEGRO_EVENT_QUEUE *queue;
13 ALLEGRO_VERTEX v[2];
14 ALLEGRO_EVENT event;
15 ALLEGRO_DISPLAY *display;
16 ALLEGRO_TIMER *timer;
17 ALLEGRO_TIMER *new_timer;
18 ALLEGRO_FONT *font;
19
20 int quit = false;
21 int redraw = false;
22 int start = false;
23 int fps = 0, frameCounter = 0;
24 int x = 30;
25 int y = 30;
26 char *message_start = "Press SPACE to start";
27 char *message_end = "Press ESCAPE to exit";
28
29 /*Initializing vertex*/
30 v[1].color = al_map_rgba_f(0, 0, 0, 0);
31 v[1].u = 0;
32 v[1].v = 0;
33 v[1].x = 500;
34 v[1].y = 300;
35 v[1].z = 0;
36 v[2].color = al_map_rgba_f(0, 0, 0, 0);
37 v[2].u = 0;
38 v[2].v = 0;
39 v[2].x = 500;
40 v[2].y = 400;
41 v[2].z = 0;
42
43 queue = al_create_event_queue();
44 display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
45 timer = al_create_timer(FPS);
46 new_timer = al_create_timer(1);
47 font = al_create_builtin_font();
48
49 al_register_event_source(queue, al_get_display_event_source(display));
50 al_register_event_source(queue, al_get_timer_event_source(timer));
51 al_register_event_source(queue, al_get_timer_event_source(new_timer));
52 al_register_event_source(queue, al_get_keyboard_event_source());
53
54 /*creating the mighty pink square*/
55
56 /*initiate display*/
57 al_set_target_bitmap(al_get_backbuffer(display));
58 al_clear_to_color(al_map_rgb(0, 0, 0));
59 al_draw_text(font, al_map_rgb(255, 0, 255), SCREEN_WIDTH / 2,
60 SCREEN_HEIGHT / 2 - 5, ALLEGRO_ALIGN_CENTER, message_start);
61 al_draw_text(font, al_map_rgb(255, 0, 255), SCREEN_WIDTH / 2,
62 SCREEN_HEIGHT / 2 + 5, ALLEGRO_ALIGN_CENTER, message_end);
63 al_flip_display();
64
65 al_start_timer(timer);
66 al_start_timer(new_timer);
67
68 while (!quit) {
69 if (redraw && start) {
70 al_set_target_bitmap(al_get_backbuffer(display));
71 al_clear_to_color(al_map_rgb(127, 127, 127));
72 /*PRINT THING HERE*/
73 al_draw_soft_line(&v[1], &v[2], (uintptr_t) NULL, NULL, NULL, drawline);
74 /******************/
75 al_draw_textf(font, al_map_rgb(0, 0, 255), SCREEN_WIDTH - 10, 10, ALLEGRO_ALIGN_RIGHT, "%d", fps);
76 al_flip_display();
77 redraw = false;
78 frameCounter++;
79 }
80
81 do {
82 al_wait_for_event(queue, &event);
83 /*timer*/
84 if (event.type == ALLEGRO_EVENT_TIMER) {
85 if (start) {
86 if (event.timer.source == timer) {
87 redraw = true;
88 }
89 if (event.timer.source == new_timer) {
90 fps = frameCounter;
91 frameCounter = 0;
92 }
93 }
94
95 }
96 /*keyboard*/
97 if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
98 if (start) {
99 if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) {
100 if (y != SCREEN_HEIGHT - SQUARE) {
101 y = y + 30;
102 }
103
104 }
105 if (event.keyboard.keycode == ALLEGRO_KEY_UP) {
106 if (y != 0) {
107 y = y - 30;
108 }
109 }
110 if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT) {
111 if (x != SCREEN_WIDTH - SQUARE) {
112 x = x + 30;
113 }
114 }
115 if (event.keyboard.keycode == ALLEGRO_KEY_LEFT) {
116 if (x != 0) {
117 x = x - 30;
118 }
119
120 }
121 }
122 if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
123 quit = true;
124 }
125 if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) {
126 start = true;
127 }
128 }
129 } while (!al_is_event_queue_empty(queue));
130
131 }
132
133 al_destroy_event_queue(queue);
134 al_destroy_display(display);
135 al_destroy_timer(timer);
136 al_destroy_timer(new_timer);
137 al_destroy_font(font);
138
139 return EXIT_SUCCESS;
140}
141
142void drawline(uintptr_t state, int x, int y) {
143
144 al_draw_line(x + 0.5, y + 0.5, x + 10.5, y + 0.5, al_map_rgb(255, 0, 255), 1);
145}
My al_draw_soft_line is at the line 73 and when I run my program it just crash. Can anyone explain to me what I do wrong? |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Arrays are zero indexed. You're accessing [1] and [2] of your ALLEGRO_VERTEX[2] array, which are off by 1. It should be v[0] and v[1], in your al_draw_soft_line call as well. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Francis Langlais
Member #15,985
June 2015
|
Now I feel stupid. But this doesn't fix the problem. Can it be a problem with my drawline function? |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Show your updated code please. What line is it crashing on? The al_draw_soft_line call? Or somewhere else? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Francis Langlais
Member #15,985
June 2015
|
1#include <stdio.h>
2#include <stddef.h>
3#include <stdbool.h>
4#include <allegro5/allegro.h>
5#include <allegro5/allegro_image.h>
6#include <allegro5/allegro_audio.h>
7#include <allegro5/allegro_acodec.h>
8#include <allegro5/allegro_font.h>
9#include <allegro5/allegro_primitives.h>
10
11#define SCREEN_WIDTH 1020
12#define SCREEN_HEIGHT 750
13#define SQUARE 30
14#define FPS 0.0166666666666667
15#define ALPHA 0.5
16#define PI 3.141592653589793
17
18void drawline(uintptr_t state, int x, int y);
19
20int main(void) {
21
22 al_init();
23 al_install_keyboard();
24 al_install_audio();
25 al_init_acodec_addon();
26 al_init_image_addon();
27 al_init_primitives_addon();
28
29 ALLEGRO_EVENT_QUEUE *queue;
30 ALLEGRO_VERTEX v[2];
31 ALLEGRO_EVENT event;
32 ALLEGRO_DISPLAY *display;
33 ALLEGRO_TIMER *timer;
34 ALLEGRO_TIMER *new_timer;
35 ALLEGRO_FONT *font;
36
37 int quit = false;
38 int redraw = false;
39 int start = false;
40 int fps = 0, frameCounter = 0;
41 int x = 30;
42 int y = 30;
43 char *message_start = "Press SPACE to start";
44 char *message_end = "Press ESCAPE to exit";
45
46 /*Initializing vertex*/
47 v[1].color = al_map_rgba_f(0, 0, 0, 0);
48 v[1].u = 0;
49 v[1].v = 0;
50 v[1].x = 500;
51 v[1].y = 300;
52 v[1].z = 0;
53 v[0].color = al_map_rgba_f(0, 0, 0, 0);
54 v[0].u = 0;
55 v[0].v = 0;
56 v[0].x = 500;
57 v[0].y = 400;
58 v[0].z = 0;
59
60 queue = al_create_event_queue();
61 display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
62 timer = al_create_timer(FPS);
63 new_timer = al_create_timer(1);
64 font = al_create_builtin_font();
65
66 al_register_event_source(queue, al_get_display_event_source(display));
67 al_register_event_source(queue, al_get_timer_event_source(timer));
68 al_register_event_source(queue, al_get_timer_event_source(new_timer));
69 al_register_event_source(queue, al_get_keyboard_event_source());
70
71 /*creating the mighty pink square*/
72
73 /*initiate display*/
74 al_set_target_bitmap(al_get_backbuffer(display));
75 al_clear_to_color(al_map_rgb(0, 0, 0));
76 al_draw_text(font, al_map_rgb(255, 0, 255), SCREEN_WIDTH / 2,
77 SCREEN_HEIGHT / 2 - 5, ALLEGRO_ALIGN_CENTER, message_start);
78 al_draw_text(font, al_map_rgb(255, 0, 255), SCREEN_WIDTH / 2,
79 SCREEN_HEIGHT / 2 + 5, ALLEGRO_ALIGN_CENTER, message_end);
80 al_flip_display();
81
82 al_start_timer(timer);
83 al_start_timer(new_timer);
84
85 while (!quit) {
86 if (redraw && start) {
87 al_set_target_bitmap(al_get_backbuffer(display));
88 al_clear_to_color(al_map_rgb(127, 127, 127));
89 /*PRINT THING HERE*/
90 al_draw_soft_line(&v[1], &v[0], (uintptr_t) NULL, NULL, NULL, drawline);
91 /******************/
92 al_draw_textf(font, al_map_rgb(0, 0, 255), SCREEN_WIDTH - 10, 10, ALLEGRO_ALIGN_RIGHT, "%d", fps);
93 al_flip_display();
94 redraw = false;
95 frameCounter++;
96 }
97
98 do {
99 al_wait_for_event(queue, &event);
100 /*timer*/
101 if (event.type == ALLEGRO_EVENT_TIMER) {
102 if (start) {
103 if (event.timer.source == timer) {
104 redraw = true;
105 }
106 if (event.timer.source == new_timer) {
107 fps = frameCounter;
108 frameCounter = 0;
109 }
110 }
111
112 }
113 /*keyboard*/
114 if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
115 if (start) {
116 if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) {
117 if (y != SCREEN_HEIGHT - SQUARE) {
118 y = y + 30;
119 }
120
121 }
122 if (event.keyboard.keycode == ALLEGRO_KEY_UP) {
123 if (y != 0) {
124 y = y - 30;
125 }
126 }
127 if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT) {
128 if (x != SCREEN_WIDTH - SQUARE) {
129 x = x + 30;
130 }
131 }
132 if (event.keyboard.keycode == ALLEGRO_KEY_LEFT) {
133 if (x != 0) {
134 x = x - 30;
135 }
136
137 }
138 }
139 if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
140 quit = true;
141 }
142 if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) {
143 start = true;
144 }
145 }
146 } while (!al_is_event_queue_empty(queue));
147
148 }
149
150 al_destroy_event_queue(queue);
151 al_destroy_display(display);
152 al_destroy_timer(timer);
153 al_destroy_timer(new_timer);
154 al_destroy_font(font);
155
156 return EXIT_SUCCESS;
157}
158
159void drawline(uintptr_t state, int x, int y) {
160
161 al_draw_line(x + 0.5, y + 0.5, x + 10.5, y + 0.5, al_map_rgb(255, 0, 255), 1);
162}
Yes, if I comment the al_draw_soft_line the program run without problem. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Allegro currently expects all 3 function pointer arguments to be non-NULL. So supply a dummy function that does nothing. 1
2void firstone(uintptr_t, int, int, ALLEGRO_VERTEX*, ALLEGRO_VERTEX*);
3void stepper(uintptr_t , int);
4void drawline(uintptr_t state, int x, int y);
5
6void firstone(uintptr_t, int, int, ALLEGRO_VERTEX*, ALLEGRO_VERTEX*) {
7
8}
9void stepper(uintptr_t , int) {
10
11}
12
13//...
14 al_draw_soft_line(&v[1], &v[0], (uintptr_t) NULL, firstone , stepper , drawline);
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|