/
demo_bitblit.c
168 lines (131 loc) · 5.02 KB
/
demo_bitblit.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*****************************************************************************
* MIDRES Library - an isomorphic gamelib for retrocomputers *
*****************************************************************************
* Copyright 2020-2021 Marco Spedaletti (asimov@mclink.it)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*----------------------------------------------------------------------------
* Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
* (la "Licenza"); è proibito usare questo file se non in conformità alla
* Licenza. Una copia della Licenza è disponibile all'indirizzo:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Se non richiesto dalla legislazione vigente o concordato per iscritto,
* il software distribuito nei termini della Licenza è distribuito
* "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
* implicite. Consultare la Licenza per il testo specifico che regola le
* autorizzazioni e le limitazioni previste dalla medesima.
****************************************************************************/
#ifdef __DEMO_BITBLIT__
/****************************************************************************
** INCLUDE SECTION
****************************************************************************/
#include <stdio.h>
#include "main.h"
#include "midres.h"
/****************************************************************************
** RESIDENT VARIABLES SECTION
****************************************************************************/
// All variables defined as global within the resident body of the code are
// accessible from all modules, both resident and changing ones.
// Width and height of the sprite, in mixels.
#define SPRITE_WIDTH 10
#define SPRITE_HEIGHT 10
// Speed of the sprite, in mixels/frame
#define SPRITE_SPEED 2
// Or with "mr_blitop_color" if you want to have a colored sprite:
// in this case, however, you will have color clashes.
#define SPRITE_COLOR mr_blitop_set
// Position of the sprite on the screen.
int x = 0, y = 0;
// Speed along abscissa and ordinate, in mixels/frame
int dx = SPRITE_SPEED, dy = SPRITE_SPEED;
// This flag is used to know if the background has been saved.
unsigned char p = 0;
// Position of last saved background.
int px = 0, py = 0;
/****************************************************************************
** RESIDENT FUNCTIONS SECTION
****************************************************************************/
// All the functions defined within the resident body of the code are
// accessible from all modules, both resident and changing ones.
// Generate a color sprite and save it on the auxiliary screen
// at (0,0)-(SPRITE_WIDTH, SPRITE_HEIGHT)
void create_sprite() {
mr_recte(0, 0, SPRITE_WIDTH-1, SPRITE_HEIGHT-1, MR_COLOR_YELLOW);
mr_linee(0, 0, SPRITE_WIDTH - 1, SPRITE_HEIGHT - 1, MR_COLOR_RED);
mr_linee(SPRITE_WIDTH - 1, 0, 0, SPRITE_HEIGHT - 1, MR_COLOR_RED);
mr_blit_from_screene(0, 0, SPRITE_WIDTH, SPRITE_HEIGHT, MR_AUX_DEFAULT, 0, 0, SPRITE_COLOR);
mr_clear_bitmape();
}
// Draw the color sprite on the screen.
void draw_sprite() {
// If needed, restore the screen. The saved background is
// at position (SPRITE_WIDTH+1,0).
if (p > 0) {
mr_blit_to_screene(MR_AUX_DEFAULT,
SPRITE_WIDTH+1, 0, SPRITE_WIDTH, SPRITE_HEIGHT,
px, py, mr_blitop_set | mr_blitop_color);
}
// Save the background.
mr_blit_from_screene( x, y, SPRITE_WIDTH, SPRITE_HEIGHT,
MR_AUX_DEFAULT, SPRITE_WIDTH+1, 0,
mr_blitop_set | mr_blitop_color);
// Draw the sprite.
mr_blit_to_screene(MR_AUX_DEFAULT, 0, 0, SPRITE_WIDTH, SPRITE_HEIGHT,
x, y,
SPRITE_COLOR);
// Save the fact we draw the sprite at position given.
p = 1;
px = x;
py = y;
}
void demo_bitblit() {
// Initialize the graphical subsystem.
mr_init();
// Enable the double buffering.
mr_doublebuffer_init();
// Clear all involved screens.
mr_clear_bitmap(MR_AUX_DEFAULT);
mr_clear_bitmapv();
mr_clear_bitmape();
// Update the frame.
mr_doublebuffer_switch();
// Create a sprite to move.
create_sprite();
// Endless loop.
while (1) {
// Draw the sprite on the screen
draw_sprite();
// Update the screen
mr_doublebuffer_switch();
// Move the sprite along the direction.
x = x + dx;
y = y + dy;
// Change direction if we are going to hit the borders.
if ((x + SPRITE_WIDTH+1) >= MR_SCREEN_WIDTH * 2) {
dx = -dx;
}
if ((y + SPRITE_HEIGHT+1) >= (MR_SCREEN_HEIGHT) * 2) {
dy = -dy;
}
if (x == 0) {
dx = -dx;
}
if (y == 0) {
dy = -dy;
}
}
}
#endif