-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbouncing.cpp
More file actions
144 lines (121 loc) · 3.69 KB
/
bouncing.cpp
File metadata and controls
144 lines (121 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <vector>
#include <algorithm>
#include<cstdlib>
using namespace std;
const float FPS = 30;
const int SCREEN_W = 1500;
const int SCREEN_H = 1000;
const int BOUNCER_SIZE = 32;
struct grid
{
int n,m;
int pixel_height, pixel_width, offset_height, offset_width;
vector<vector<int> > v;
grid(int n, int m, int ph,int pw,int oh,int ow)
:n(n),m(m),pixel_height(ph),pixel_width(pw),offset_height(oh),offset_width(ow)
{
v = vector<vector<int> >(n);
for(int i=0;i<n;i++)
v[i]=vector<int>(m);
}
pair<int,int> translate(int a, int b)
{
return make_pair(offset_width+ b*(pixel_width/m),offset_height+a*(pixel_height/n));
}
void draw()
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
al_draw_filled_rectangle(translate(i,j).first,
translate(i,j).second,
translate(i+1,j+1).first,
translate(i+1,j+1).second,
al_map_rgb(v[i][j]*200+50,v[i][j]*255,v[i][j]*255));
}
void alter()
{
v[rand()%n][rand()%m]^=1;
}
};
string get_statistics(grid & g)
{
int wh=0;
for(int i=0;i<g.n;i++) for(int j=0;j<g.m;j++)
if(g.v[i][j])
wh++;
return to_string(wh) + " white cells " + to_string(g.n*g.m-wh) + " red cells ";
}
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
timer = al_create_timer(1.0 / FPS);
if(!timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
display = al_create_display(SCREEN_W, SCREEN_H);
if(!display) {
fprintf(stderr, "failed to create display!\n");
al_destroy_timer(timer);
return -1;
}
al_init_primitives_addon();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT *font = al_load_ttf_font("/usr/share/fonts/truetype/custom/monaco.ttf",28,0 );
if (!font){
fprintf(stderr, "Could not load font.\n");
return -1;
}
al_set_target_bitmap(al_get_backbuffer(display));
event_queue = al_create_event_queue();
if(!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(timer);
bool redraw=false;
grid G = grid(10,10,1000,1000,0,0);
while(1)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER) {
G.alter();
redraw=true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
if(redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
al_clear_to_color(al_map_rgb(0,0,0));
G.draw();
al_draw_text(font,
al_map_rgb(255,255,255),
1050,50,ALLEGRO_ALIGN_LEFT, get_statistics(G).c_str());
al_flip_display();
}
}
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}