|
RPG Player |
motyas
Member #16,305
April 2016
|
Sorry for my insistence guys, I know that I'm bothering so much, and that this is my third post this week! :C I would like you tell me if is best that I don't ask so much... 1#include <allegro5/allegro.h>
2#include <allegro5/allegro_image.h>
3#include <allegro5/allegro_native_dialog.h>
4#include <allegro5/allegro_primitives.h>
5#include <allegro5/allegro_font.h>
6#include <allegro5/allegro_ttf.h>
7
8const int screenWidth = 800, screenHeight = 572;
9const float FPS = 60.0;
10enum direction {
11 UP, DOWN, LEFT, RIGHT
12};
13
14int main(int argc, char *argv[]){
15
16 ALLEGRO_DISPLAY * display = NULL;
17 ALLEGRO_EVENT_QUEUE * event_queue = NULL;
18 ALLEGRO_TIMER * timer = NULL;
19 ALLEGRO_KEYBOARD_STATE keyState;
20 ALLEGRO_BITMAP * backImage = NULL;
21
22 float x = 320, y = 240, moveSpeed = 5;
23
24 int dir = DOWN, sourceX = 32, sourceY = 0;
25
26 bool done = false, draw = true, active = false;
27
28 if(!al_init()){
29 al_show_native_message_box(NULL, "Error", NULL, "Allegro couldn't initialize successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR);
30 return -1;
31 }
32
33 al_install_keyboard();
34 if(!al_install_keyboard()){
35 al_show_native_message_box(NULL, "Error", NULL, "couldn't initialize keyboard successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR);
36 return -1;
37 }
38
39 display = al_create_display(screenWidth, screenHeight);
40 al_set_window_title(display, "Keyboard image");
41
42 al_clear_to_color(al_map_rgb(0, 0, 0));
43
44 if(!display){
45 al_show_native_message_box(NULL, "Error", NULL, "Window wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR);
46 return -1;
47 }
48
49 event_queue = al_create_event_queue();
50 if(!event_queue){
51 al_show_native_message_box(NULL, "Error", NULL, "Event queue wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR);
52 return -1;
53 }
54
55 timer = al_create_timer(1.0 / FPS);
56 if(!timer){
57 al_show_native_message_box(NULL, "Error", NULL, "Timer wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR);
58 return -1;
59 }
60 al_init_image_addon();
61
62 backImage = al_load_bitmap("/Users/matias/Desktop/Matías/C++/Programas/AllegroFiles/Tutorial/Allegro_Keyboard/Allegro_Keyboard/resources/backGame.bmp");
63
64 ALLEGRO_BITMAP * player = NULL;
65 player = al_load_bitmap("/Users/matias/Desktop/Matías/C++/Programas/AllegroFiles/Tutorial/Allegro_Keyboard/Allegro_Keyboard/resources/player.png");
66
67 al_init_primitives_addon();
68
69 al_register_event_source(event_queue, al_get_display_event_source(display));
70 al_register_event_source(event_queue, al_get_timer_event_source(timer));
71 al_register_event_source(event_queue, al_get_keyboard_event_source());
72
73 al_start_timer(timer);
74
75 while(!done){
76 ALLEGRO_EVENT ev;
77 al_wait_for_event(event_queue, &ev);
78 al_get_keyboard_state(&keyState);
79
80 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
81 done = true;
82 }
83 else if(ev.type == ALLEGRO_EVENT_TIMER){
84
85 active = true;
86
87 if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)){
88 y += moveSpeed;
89 dir = DOWN;
90 }
91 else if(al_key_down(&keyState, ALLEGRO_KEY_UP)){
92 y -= moveSpeed;
93 dir = UP;
94 }
95 else if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT)){
96 x += moveSpeed;
97 dir = RIGHT;
98 }
99 else if(al_key_down(&keyState, ALLEGRO_KEY_LEFT)){
100 x -= moveSpeed;
101 dir = LEFT;
102 }
103 else{
104 active = false;
105 }
106
107 if(active){
108 sourceX += al_get_bitmap_width(player) / 3;
109 }
110 else{
111 sourceX = 32;
112 }
113
114 if(sourceX >= al_get_bitmap_width(player)){
115 sourceX = 0;
116 }
117
118 sourceY = dir;
119
120 draw = true;
121 }
122
123 if(draw){
124 al_draw_bitmap(backImage, 0, 0, NULL);
125 al_draw_bitmap_region(player, sourceX, sourceY * al_get_bitmap_height(player) / 4, 32, 32, x, y, NULL);
126 al_flip_display();
127 al_clear_to_color(al_map_rgb(0, 0, 0));
128 }
129 }
130
131 al_destroy_bitmap(player);
132 al_destroy_timer(timer);
133 al_destroy_display(display);
134 al_destroy_event_queue(event_queue);
135
136 return 0;
137}
Thanks! |
amarillion
Member #940
January 2001
|
Why do you initialise sourceX at 32? -- |
motyas
Member #16,305
April 2016
|
I don't know, in the tutorial that i've saw the code is like this... I tried changing the values but nothing... What could be? Someone know how i could do a RPG player? Thanks : ) |
Audric
Member #907
January 2001
|
The original example must have used a character of width 32, so sourceX=32 picks the "middle" column, where the character has both feet on floor. Try using the attached image, I re-aligned your character, individual sprites are now 39x48. It's a GIF, I couldn't find a good program for 32bit PNG at the moment - I hope it will load OK on OSX. int dir = DOWN, sourceX = 32, sourceY = 0; sourceX = 32; al_draw_bitmap_region(player, sourceX, sourceY * al_get_bitmap_height(player) / 4, 32, 32, x, y, NULL); |
motyas
Member #16,305
April 2016
|
It works, but, when i move to the RIGHT the animation go UP, when I move DOWN, the animation go to the LEFT, when I move UP, the animation go DOWN and when I move to RIGHT the animation go up, do you know what could be? |
Audric
Member #907
January 2001
|
The original example must also have the sprites ordered differently... |
motyas
Member #16,305
April 2016
|
Oh man, thank you very much! |
|