1 /*         ______   ___    ___
2  *        /\  _  \ /\_ \  /\_ \
3  *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___
4  *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
5  *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
6  *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7  *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8  *                                           /\____/
9  *                                           \_/__/
10  *
11  *      A simple C++ test.
12  *
13  *      By Henrik Stokseth.
14  *
15  *      See readme.txt for copyright information.
16  */
17 
18 
19 #include <stdlib.h>
20 #include <time.h>
21 #include "allegro.h"
22 
23 #define RODENTS  4
24 
25 
26 /* We define dumb memory management operators in order
27  * not to have to link against libstdc++ with GCC 3.x.
28  * Don't do that in real-life C++ code unless you know
29  * what you're doing.
30  */
operator new(size_t size)31 void *operator new(size_t size)
32 {
33    return malloc(size);
34 }
35 
operator delete(void * ptr)36 void operator delete(void *ptr)
37 {
38   if (ptr)
39     free (ptr);
40 }
41 
42 
43 /* Our favorite pet. */
44 class rodent {
45    public:
46       rodent(BITMAP *bmp);
47       void move();
48       void draw(BITMAP *bmp);
49    private:
50       int x, y;
51       int delta_x, delta_y;
52       BITMAP *sprite;
53 };
54 
55 
rodent(BITMAP * bmp)56 rodent::rodent(BITMAP *bmp)
57 {
58    x = rand() % (SCREEN_W-bmp->w);
59    y = rand() % (SCREEN_H-bmp->h);
60 
61    do {
62      delta_x = (rand() % 11) - 5;
63    } while(!delta_x);
64 
65    do {
66       delta_y = (rand() % 11) - 5;
67    } while(!delta_y);
68 
69    sprite = bmp;
70 }
71 
72 
move()73 void rodent::move()
74 {
75    if((x+sprite->w+delta_x >= SCREEN_W) || (x+delta_x < 0)) delta_x = -delta_x;
76    if((y+sprite->h+delta_y >= SCREEN_H) || (y+delta_y < 0)) delta_y = -delta_y;
77 
78    x += delta_x;
79    y += delta_y;
80 }
81 
82 
draw(BITMAP * bmp)83 void rodent::draw(BITMAP *bmp)
84 {
85    draw_sprite(bmp, sprite, x, y);
86 }
87 
88 
89 /* A little counter to waste your time. */
90 volatile int counter = 0;
91 
my_timer_handler()92 void my_timer_handler()
93 {
94    counter++;
95 }
96 END_OF_FUNCTION(my_timer_handler)
97 
98 
99 /* Yup, you read correctly, we're creating the World here. */
100 class world {
101    public:
102       world();   /* Genesis */
103       ~world();  /* Apocalypse */
104       void draw();
105       void logic();
106       void loop();
107    private:
108       BITMAP *dbuffer;
109       BITMAP *mouse_sprite;
110       int active;
111       rodent *mouse[RODENTS];
112 };
113 
114 
world()115 world::world()
116 {
117    PALETTE pal;
118    active = TRUE;
119 
120    dbuffer = create_bitmap(SCREEN_W, SCREEN_H);
121    mouse_sprite = load_bitmap("../examples/mysha.pcx", pal);
122 
123    if (!mouse_sprite) {
124       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
125       allegro_message("Error loading bitmap\n%s\n", allegro_error);
126       exit(1);
127    }
128 
129    set_palette(pal);
130 
131    for(int what_mouse=0; what_mouse < RODENTS; what_mouse++)
132       mouse[what_mouse] = new rodent(mouse_sprite);
133 }
134 
135 
~world()136 world::~world()
137 {
138    destroy_bitmap(dbuffer);
139    destroy_bitmap(mouse_sprite);
140 
141    for(int what_mouse=0; what_mouse < RODENTS; what_mouse++)
142       delete mouse[what_mouse];
143 }
144 
145 
draw()146 void world::draw()
147 {
148    clear_bitmap(dbuffer);
149 
150    for(int what_mouse=0; what_mouse < RODENTS; what_mouse++)
151       mouse[what_mouse]->draw(dbuffer);
152 
153    blit(dbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
154 }
155 
156 
logic()157 void world::logic()
158 {
159    if(key[KEY_ESC]) active = FALSE;
160 
161    for(int what_mouse=0; what_mouse < RODENTS; what_mouse++)
162       mouse[what_mouse]->move();
163 }
164 
165 
loop()166 void world::loop()
167 {
168    install_int_ex(my_timer_handler, BPS_TO_TIMER(10));
169 
170    while(active) {
171       while(counter > 0) {
172          counter--;
173          logic();
174       }
175       draw();
176    }
177 
178    remove_int(my_timer_handler);
179 }
180 
181 
main()182 int main()
183 {
184    allegro_init();
185 
186    install_keyboard();
187    install_timer();
188 
189    srand(time(NULL));
190    LOCK_VARIABLE(counter);
191    LOCK_FUNCTION(my_timer_handler);
192 
193    if (set_gfx_mode(GFX_AUTODETECT, 1024, 768, 0, 0) != 0) {
194       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
195       allegro_message("Error setting graphics mode\n%s\n", allegro_error);
196       return 1;
197    }
198 
199    world *game = new world();  /* America! America! */
200    game->loop();
201    delete game;
202 
203    return 0;
204 }
205 END_OF_MAIN()
206