Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Drawing instead of falling

This thread is locked; no one can reply to it. rss feed Print
Drawing instead of falling
Vanneto
Member #8,643
May 2007

Hello! I have this code.

1#include <allegro.h>
2#include <stdlib.h>
3#include "data_file.h"
4#pragma hdrstop
5 
6 
7int main(int argc, char **argv[])
8{
9 if(allegro_init() != 0) exit(EXIT_FAILURE);
10 install_keyboard();
11
12 set_color_depth(32);
13 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
14
15 // Create a buffer for smoothness
16 BITMAP *buffer = create_bitmap(640, 480);
17
18 // Load the datafile
19 DATAFILE *data = NULL;
20 data = load_datafile("data.dat");
21
22 // Error?
23 if(data == NULL)
24 {
25 exit(EXIT_FAILURE);
26 }
27
28 int player_x,player_y;
29 BITMAP* player = (BITMAP*)data[data_player].dat;
30 BITMAP* backgr = (BITMAP*)data[data_background].dat;
31 BITMAP* floor = (BITMAP*)data[data_floor].dat;
32
33 player_x=SCREEN_W/2;
34 player_y=SCREEN_H/2;
35
36 // If the user doesent press escape, do something
37 while(!key[KEY_ESC])
38 {
39 // The movements, player can move left and right, but cannot
40 // move beyond the boundaries of the scree
41
42 int FLOOR = SCREEN_H - 20;
43 if(player_y != FLOOR)
44 {
45 while(player_y < FLOOR)
46 {
47 if(key[KEY_RIGHT])
48 {
49 player_x = player_x + 1;
50 }
51 if(key[KEY_LEFT])
52 {
53 player_x = player_x - 1;
54 }
55 player_y = player_y + 5;
56 draw_sprite(screen, player, player_x, player_y);
57 rest(2);
58 }
59 }
60 if(key[KEY_UP])
61 {
62 while(key[KEY_UP])
63 {
64 if(key[KEY_RIGHT])
65 {
66 player_x = player_x + 1;
67 }
68 if(key[KEY_LEFT])
69 {
70 player_x = player_x - 1;
71 }
72 player_y = player_y - 2;
73 draw_sprite(screen, player, player_x, player_y);
74 rest(1);
75 }
76 }
77 // BORDERS
78 if(player_x == SCREEN_W - 20)
79 {
80 player_x = player_x - 2;
81 rest(1);
82 }
83 if(player_x == 0)
84 {
85 player_x = player_x + 2;
86 rest(1);
87 }
88 if(player_y == SCREEN_H - 20)
89 {
90 player_y = player_y - 2;
91 rest(1);
92 }
93 if(player_y == 0)
94 {
95 player_y = player_y + 2;
96 rest(1);
97 }
98 if(key[KEY_RIGHT])
99 {
100 player_x = player_x + 2;
101 rest(1);
102 }
103 if(key[KEY_LEFT])
104 {
105 player_x = player_x - 2;
106 rest(1);
107 }
108 /*if(key[KEY_UP])
109 {
110 player_y = player_y -2;
111 rest(1);
112 }
113 if(key[KEY_DOWN])
114 {
115 player_y = player_y + 2;
116 rest(1);
117 }*/
118 draw_sprite(buffer, player, player_x,player_y);
119
120 // Draw the buffer to the screen
121 blit(buffer, screen, 0, 0, 0, 0, 640,480);
122 clear_bitmap(buffer); // So it doesent draw, but move :)
123 }
124 // Destroy bitmaps and unloads datafiles
125 destroy_bitmap(buffer);
126 unload_datafile(data);
127 return 0;
128}
129END_OF_MAIN();

If you compile it, you will se that you can move LEFT/RIGHT and jump. BUT when you jump and fall then there is a line drawn. Instead of a new cube each frame, the previous is still visible. Compile it and youll see what I mean.

What do to? I tried clear_bitmap(player); / clear_bitmap(screen); . I dont know what to do. And yeah, the background and the floor arent visible either! :P I have included source and .data file!

Thanks in advance for the help! Im new in this, just trying out the basics! :)

In capitalist America bank robs you.

miran
Member #2,407
June 2002

Doesn't compile:

move_char.cpp:3:23: error: data_file.h: No such file or directory
move_char.cpp: In function ‘int main(int, char***):
move_char.cpp:29: error: ‘data_player’ was not declared in this scope
move_char.cpp:30: error: ‘data_background’ was not declared in this scope
move_char.cpp:31: error: ‘data_floor’ was not declared in this scope

Btw, very original way to use the rest function. Did you figure this out yourself or did someone tell you? Because it's wrong. You need to use timers instead.

As for your problem, you should read about double buffering again. Because you're not doing it right. You should draw the scene to the backbuffer and then blit that to the screen.

Also you should separate logic from drawing.

--
sig used to be here

Go to: