-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apps: new app cube - rotating cube outline
- Loading branch information
1 parent
7f483ea
commit 3e92471
Showing
9 changed files
with
316 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
SHELL := /bin/sh | ||
.ONESHELL: | ||
MAKEFLAGS += --warn-undefined-variables | ||
MAKEFLAGS += --no-builtin-rules | ||
|
||
all: CUBE.COM | ||
|
||
.PHONY: CUBE.COM | ||
CUBE.COM: | ||
@ez80-cc --lib v99x8-standard --output-dir ../bin CUBE.COM cube.c | ||
|
||
# Change lib to v99x8-hdmi for tang nano V9958 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env bash | ||
|
||
source ~/.ez80-clang | ||
|
||
SCRIPT_DIR=$(dirname "$0") | ||
SCRIPT_DIR=$(cd "${SCRIPT_DIR}/" && pwd)/../../clang-tool-chain/ | ||
|
||
# export EZ80_CLANG_SYSTEM_INCLUDE_PATH=${SCRIPT_DIR}/include | ||
# export EZ80_CLANG_LIB_PATH=${SCRIPT_DIR}/lib/ | ||
# export EZ80_CLANG_LS_PATH=${SCRIPT_DIR}/linker-scripts/ | ||
|
||
# export INCLUDE_PATHS="-isystem ${SCRIPT_DIR}/include -isystem ${SCRIPT_DIR}/working/llvm-project/build/lib/clang/15.0.0/include/" | ||
# PATH="${SCRIPT_DIR}working/llvm-project/build/bin:${SCRIPT_DIR}working/opt/ez80-none-elf/bin/:${PATH}" | ||
|
||
# shopt -s expand_aliases | ||
# alias ez80-clang="clang -nostdinc" | ||
# alias ez80-as="ez80-none-elf-as" | ||
# alias ez80-ld="ez80-none-elf-ld" | ||
|
||
|
||
set -e | ||
|
||
echo "Compiling cube.c -> cube.s" | ||
ez80-clang ${INCLUDE_PATHS} -nostdinc -ffunction-sections -fdata-sections -Oz -Wall -Wextra -Wunreachable-code -Werror -mllvm -z80-print-zero-offset -S cube.c -c -o cube.s | ||
|
||
echo "Compiling cube.s -> cube.o" | ||
ez80-as -march=ez80+full -a=./cube.lst ./cube.s -o ./cube.o | ||
|
||
ez80-ld -T /opt/ez80-clang/linker-scripts/cpm-64k.ld --relax -O1 --strip-discarded --orphan-handling=error -L/opt/ez80-clang/lib/ -lcrt cube.o --start-group -llibc -lcrt -lcpm -lv99x8-standard -lez80 --end-group --oformat binary -o ../bin/CUBE.COM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#include <cpm.h> | ||
#include <math.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <v99x8.h> | ||
|
||
#define LEFT 0 | ||
#define RIGHT 255 | ||
#define TOP 0 | ||
#define BOTTOM (212) | ||
#define WIDTH (RIGHT - LEFT) | ||
#define HEIGHT (BOTTOM - TOP) | ||
|
||
void erase_page_0() { vdp_cmd_vdp_to_vram(LEFT, TOP, WIDTH, HEIGHT, 0, 0); } | ||
|
||
void erase_page_1() { vdp_cmd_vdp_to_vram(LEFT, TOP + 256, WIDTH, HEIGHT, 0, 0); } | ||
|
||
uint8_t current_page = 0; | ||
|
||
void erase_current_page() { | ||
if (current_page == 0) { | ||
erase_page_0(); | ||
} else { | ||
erase_page_1(); | ||
} | ||
} | ||
|
||
void page_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t color) { | ||
if (current_page == 0) { | ||
vdp_draw_line(x1 + LEFT, y1 + TOP, (x2), y2, color, CMD_LOGIC_IMP); | ||
} else { | ||
vdp_draw_line(x1 + LEFT, y1 + TOP + 256, x2, y2 + 256, color, CMD_LOGIC_IMP); | ||
} | ||
} | ||
|
||
void swap_page() { | ||
if (current_page == 0) { | ||
current_page = 1; | ||
vdp_set_page(0); | ||
} else { | ||
current_page = 0; | ||
vdp_set_page(1); | ||
} | ||
} | ||
|
||
typedef struct { | ||
float x; | ||
float y; | ||
float z; | ||
} POINT3D; | ||
|
||
const float SPEED_X = 0.05; // rps | ||
const float SPEED_Y = 0.15; // rps | ||
const float SPEED_Z = 0.10; // rps | ||
|
||
#define cx (WIDTH / 2) | ||
#define cy (HEIGHT / 2) | ||
#define cz (0) | ||
#define size (HEIGHT / 4) | ||
|
||
#define VERTICES 8 | ||
POINT3D vertices[VERTICES] = {{cx - size, cy - size, cz - size}, {cx + size, cy - size, cz - size}, | ||
{cx + size, cy + size, cz - size}, {cx - size, cy + size, cz - size}, | ||
{cx - size, cy - size, cz + size}, {cx + size, cy - size, cz + size}, | ||
{cx + size, cy + size, cz + size}, {cx - size, cy + size, cz + size}}; | ||
|
||
#define EDGES 12 | ||
uint8_t edges[EDGES][2] = { | ||
{0, 1}, {1, 2}, {2, 3}, {3, 0}, // back face | ||
{4, 5}, {5, 6}, {6, 7}, {7, 4}, // front face | ||
{0, 4}, {1, 5}, {2, 6}, {3, 7} // connecting sides | ||
}; | ||
|
||
int24_t timeDelta, timeLast = 0; | ||
|
||
void draw_cube(int24_t timeNow) { | ||
|
||
// calculate the time difference | ||
timeDelta = timeNow - timeLast; | ||
timeLast = timeNow; | ||
|
||
erase_current_page(); | ||
|
||
// rotate the cube along the z axis | ||
float angle = timeDelta * 0.001 * SPEED_Z * M_PI * 2; | ||
for (int i = 0; i < VERTICES; i++) { | ||
POINT3D *v = &vertices[i]; | ||
float dx = v->x - cx; | ||
float dy = v->y - cy; | ||
float x = dx * cos(angle) - dy * sin(angle); | ||
float y = dx * sin(angle) + dy * cos(angle); | ||
v->x = x + cx; | ||
v->y = y + cy; | ||
} | ||
|
||
// rotate the cube along the x axis | ||
angle = timeDelta * 0.001 * SPEED_X * M_PI * 2; | ||
for (int i = 0; i < VERTICES; i++) { | ||
POINT3D *v = &vertices[i]; | ||
float dy = v->y - cy; | ||
float dz = v->z - cz; | ||
float y = dy * cos(angle) - dz * sin(angle); | ||
float z = dy * sin(angle) + dz * cos(angle); | ||
v->y = y + cy; | ||
v->z = z + cz; | ||
} | ||
|
||
// rotate the cube along the y axis | ||
angle = timeDelta * 0.001 * SPEED_Y * M_PI * 2; | ||
for (int i = 0; i < VERTICES; i++) { | ||
POINT3D *v = &vertices[i]; | ||
float dx = v->x - cx; | ||
float dz = v->z - cz; | ||
float x = dz * sin(angle) + dx * cos(angle); | ||
float z = dz * cos(angle) - dx * sin(angle); | ||
v->x = x + cx; | ||
v->z = z + cz; | ||
} | ||
|
||
// draw each edge | ||
for (int i = 0; i < EDGES; i++) { | ||
uint8_t *edge = edges[i]; | ||
page_draw_line(vertices[edge[0]].x, vertices[edge[0]].y, vertices[edge[1]].x, vertices[edge[1]].y, 255); | ||
} | ||
|
||
swap_page(); | ||
} | ||
|
||
int main(/*const int argc, const char *argv[]*/) { | ||
|
||
vdp_set_mode(7, 192, PAL); | ||
vdp_erase_bank0(0); | ||
vdp_erase_bank1(0); | ||
erase_page_0(); | ||
erase_page_1(); | ||
|
||
vdp_set_page(0); | ||
|
||
uint24_t time = 0; | ||
while (true) { | ||
if (cpm_rawio() != 0) | ||
return 0; | ||
draw_cube(time); | ||
time += 10; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Cube</title> | ||
<style> | ||
body { | ||
margin: 0; | ||
} | ||
canvas { | ||
display: block; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<script> | ||
// constants | ||
const COLOR_BG = "black"; | ||
const COLOR_CUBE = "yellow"; | ||
const SPEED_X = 0.05; // rps | ||
const SPEED_Y = 0.15; // rps | ||
const SPEED_Z = 0.10; // rps | ||
const POINT3D = function(x, y, z) { this.x = x; this.y = y; this.z = z; }; | ||
|
||
// set up the canvas and context | ||
var canvas = document.createElement("canvas"); | ||
document.body.appendChild(canvas); | ||
var ctx = canvas.getContext("2d"); | ||
|
||
// dimensions | ||
var h = document.documentElement.clientHeight; | ||
var w = document.documentElement.clientWidth; | ||
canvas.height = h; | ||
canvas.width = w; | ||
|
||
// colours and lines | ||
ctx.fillStyle = COLOR_BG; | ||
ctx.strokeStyle = COLOR_CUBE; | ||
ctx.lineWidth = w / 100; | ||
ctx.lineCap = "round"; | ||
|
||
// cube parameters | ||
var cx = w / 2; | ||
var cy = h / 2; | ||
var cz = 0; | ||
var size = h / 4; | ||
var vertices = [ | ||
new POINT3D(cx - size, cy - size, cz - size), | ||
new POINT3D(cx + size, cy - size, cz - size), | ||
new POINT3D(cx + size, cy + size, cz - size), | ||
new POINT3D(cx - size, cy + size, cz - size), | ||
new POINT3D(cx - size, cy - size, cz + size), | ||
new POINT3D(cx + size, cy - size, cz + size), | ||
new POINT3D(cx + size, cy + size, cz + size), | ||
new POINT3D(cx - size, cy + size, cz + size) | ||
]; | ||
var edges = [ | ||
[0, 1], [1, 2], [2, 3], [3, 0], // back face | ||
[4, 5], [5, 6], [6, 7], [7, 4], // front face | ||
[0, 4], [1, 5], [2, 6], [3, 7] // connecting sides | ||
]; | ||
|
||
// set up the animation loop | ||
var timeDelta, timeLast = 0; | ||
requestAnimationFrame(loop); | ||
|
||
function loop(timeNow) { | ||
|
||
// calculate the time difference | ||
timeDelta = timeNow - timeLast; | ||
timeLast = timeNow; | ||
|
||
// background | ||
ctx.fillRect(0, 0, w, h); | ||
|
||
// rotate the cube along the z axis | ||
let angle = timeDelta * 0.001 * SPEED_Z * Math.PI * 2; | ||
for (let v of vertices) { | ||
let dx = v.x - cx; | ||
let dy = v.y - cy; | ||
let x = dx * Math.cos(angle) - dy * Math.sin(angle); | ||
let y = dx * Math.sin(angle) + dy * Math.cos(angle); | ||
v.x = x + cx; | ||
v.y = y + cy; | ||
} | ||
|
||
// rotate the cube along the x axis | ||
angle = timeDelta * 0.001 * SPEED_X * Math.PI * 2; | ||
for (let v of vertices) { | ||
let dy = v.y - cy; | ||
let dz = v.z - cz; | ||
let y = dy * Math.cos(angle) - dz * Math.sin(angle); | ||
let z = dy * Math.sin(angle) + dz * Math.cos(angle); | ||
v.y = y + cy; | ||
v.z = z + cz; | ||
} | ||
|
||
// rotate the cube along the y axis | ||
angle = timeDelta * 0.001 * SPEED_Y * Math.PI * 2; | ||
for (let v of vertices) { | ||
let dx = v.x - cx; | ||
let dz = v.z - cz; | ||
let x = dz * Math.sin(angle) + dx * Math.cos(angle); | ||
let z = dz * Math.cos(angle) - dx * Math.sin(angle); | ||
v.x = x + cx; | ||
v.z = z + cz; | ||
} | ||
|
||
// draw each edge | ||
for (let edge of edges) { | ||
ctx.beginPath(); | ||
ctx.moveTo(vertices[edge[0]].x, vertices[edge[0]].y); | ||
ctx.lineTo(vertices[edge[1]].x, vertices[edge[1]].y); | ||
ctx.stroke(); | ||
} | ||
|
||
// call the next frame | ||
requestAnimationFrame(loop); | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.