-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincludeos_svga_pythonic++.py
More file actions
85 lines (83 loc) · 2.3 KB
/
includeos_svga_pythonic++.py
File metadata and controls
85 lines (83 loc) · 2.3 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
# Copyright 2015 Oslo and Akershus University College of Applied Sciences and Alfred Bratterud Licensed under the Apache License
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
## compile with: `./rebuild.py --includeos --svga includeos_svga_pythonic++.py`
## note: this requires qemu with qxl enabled, from your qemu source folder run: ./configure --enable-spice
## for help installing qxl spice see: https://chrisrjones.com/articles/build-qemu-with-spice-video-support-for-an-os-x-vm
with c++:
import <timers>
import <array>
import <cmath>
import <deque>
import <x86intrin.h>
using namespace std::chrono
static uint8_t backbuffer[320*200] __attribute__((aligned(16)))
def set_pixel(int x, int y, uint8_t cl):
if x >= 0 && x < 320 && y >= 0 && y < 200:
backbuffer[y * 320 + x] = cl
def clear():
memset(backbuffer, 0, sizeof(backbuffer))
static int timev = 0
class Star:
float x, y
uint8_t cl
Star() {
x = rand() % 320
y = rand() % 200
cl = 18 + rand() % 14
}
def render():
uint8_t dark = std::max(cl - 6, 16)
set_pixel(x+1, y, dark)
set_pixel(x-1, y, dark)
set_pixel(x, y+1, dark)
set_pixel(x, y-1, dark)
set_pixel(x, y, cl)
def modulate():
clear()
if cl == 16:
return
float dx = (x - 160)
float dy = (y - 100)
if dx == 0 && dy == 0:
return
float mag = 1.0f / sqrtf(dx*dx + dy*dy)
dx *= mag
dy *= mag
x += dx * 1.0f
y += dy * 1.0f
render();
def clear():
set_pixel(x+1, y, 0)
set_pixel(x-1, y, 0)
set_pixel(x, y+1, 0)
set_pixel(x, y-1, 0)
set_pixel(x, y, 0)
static std::deque<Star> stars
@module( mymodule )
def start():
print("starting stars....")
VGA_gfx::set_mode(VGA_gfx::MODE_320_200_256)
VGA_gfx::clear()
VGA_gfx::apply_default_palette()
clear()
auto update_callback = def[](int):
print("updating stars....")
## add new (random) star
if rand() % 2 == 0:
Star star
star.render()
stars.push_back(star)
## render screen
VGA_gfx::blit_from(backbuffer)
## work on backbuffer
for (auto& star : stars) { star.modulate(); }
if stars.size() > 50:
auto& dead_star = stars.front()
dead_star.clear()
stars.pop_front()
timev++
while 1:
update_callback(0)
##Timers::periodic(16ms, update_callback)
import mymodule
mymodule.start()