-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarfield.cpp
More file actions
112 lines (91 loc) · 4.31 KB
/
starfield.cpp
File metadata and controls
112 lines (91 loc) · 4.31 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
/*
starfield.cpp
Classic Invaders
Copyright (c) 2013, Todd Steinackle, Simon Que
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of The No Quarter Arcade (http://www.noquarterarcade.com/) nor the names of
its contributors may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "starfield.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <DuinoCube.h>
#include "printf.h"
#include "screen.h"
#define MAX_LINE_SIZE 64
#define TILEMAP_WIDTH 32
#define TILEMAP_HEIGHT 32
static uint16_t g_vram_offset = 0;
// Generates randomized starfield tiles and tilemap.
void generate_starfield(Graphics::Screen* screen,
uint8_t layer, uint8_t palette,
uint8_t tile_width, uint8_t tile_height,
uint8_t num_tiles, uint16_t num_stars,
uint8_t min_brightness, uint8_t max_brightness) {
if (tile_width > MAX_LINE_SIZE) {
printf_P("Starfield tiles cannot be wider than %u pixels.\n",
MAX_LINE_SIZE);
return;
}
if (max_brightness < min_brightness) {
printf_P(" Min brightness must be lower than max brightness.\n");
return;
}
// Generate starfield tiles in the second bank of VRAM.
uint16_t vram_base = VRAM_BANK_SIZE + g_vram_offset;
DC.Core.writeWord(REG_SYS_CTRL, (1 << REG_SYS_CTRL_VRAM_ACCESS));
DC.Core.writeWord(REG_MEM_BANK,
VRAM_BANK_BEGIN + vram_base / VRAM_BANK_SIZE);
// Generate the tiles.
for (uint8_t i = 0; i < num_tiles; ++i) {
printf_P("Writing %d bytes of starfield data to 0x%04x\n",
tile_width * tile_height, vram_base + g_vram_offset);
uint8_t buffer[MAX_LINE_SIZE];
for (uint8_t y = 0; y < tile_height; ++y, g_vram_offset += tile_width) {
memset(buffer, 0, tile_width);
for (uint8_t x = 0; x < tile_width; ++x) {
uint16_t rand_num = rand() % (tile_width * tile_height);
if (rand_num >= num_stars) {
continue;
}
uint8_t brightness = min_brightness +
(uint8_t)rand() % (max_brightness - min_brightness + 1);
buffer[x] = brightness;
}
DC.Core.writeData(VRAM_BASE + g_vram_offset, buffer, tile_width);
}
}
DC.Core.writeWord(REG_SYS_CTRL, (0 << REG_SYS_CTRL_VRAM_ACCESS));
DC.Core.writeWord(REG_MEM_BANK, TILEMAP_BANK);
for (uint8_t y = 0; y < TILEMAP_HEIGHT; ++y) {
uint16_t buffer[TILEMAP_WIDTH];
for (uint8_t x = 0; x < TILEMAP_WIDTH; ++x)
buffer[x] = rand() % num_tiles;
screen->set_tilemap_data(layer, 0, y, buffer, sizeof(buffer));
}
// Set up grayscale palette.
for (uint8_t color = 0; true ; ++color) {
screen->set_palette_entry(palette, color, color, color, color);
if (color == 0xff)
break;
}
// Set up and enable the tile layer and tile map.
screen->setup_tile_layer(layer, true, palette, vram_base, 0);
}