Skip to content

Commit b5b5fb9

Browse files
committed
Update nGL
- Use lcd_blit everywhere - Use VECTOR3 type - Remove unnecessary ';' - Convert Makefile to unix line endings - Add Makefile.pc and libndls.h stub to enable SDL port
1 parent 818dff7 commit b5b5fb9

File tree

16 files changed

+337
-102
lines changed

16 files changed

+337
-102
lines changed

Makefile

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
GCC = nspire-gcc
2-
GPP = nspire-g++
3-
LD = nspire-ld
4-
GENZEHN = genzehn
5-
OPTIMIZE ?= fast
6-
GCCFLAGS = -O$(OPTIMIZE) -I nGL -I . -Wall -W -marm -ffast-math -mcpu=arm926ej-s -fno-math-errno -fomit-frame-pointer -flto -fno-rtti -fgcse-sm -fgcse-las -funsafe-loop-optimizations -fno-fat-lto-objects -frename-registers -fprefetch-loop-arrays -Wold-style-cast -mno-thumb-interwork -ffunction-sections -fdata-sections
7-
LDFLAGS = -lm -Wl,--gc-sections
8-
ZEHNFLAGS = --name "Crafti" --version 11 --author "Fabian Vogt" --notice "3D Minecraft" --compress
9-
EXE = crafti
10-
OBJS = $(patsubst %.c, %.o, $(shell find . -name \*.c))
11-
OBJS += $(patsubst %.cpp, %.o, $(shell find . -name \*.cpp))
12-
OBJS += $(patsubst %.S, %.o, $(shell find . -name \*.S))
13-
14-
all: $(EXE).tns
15-
16-
%.o: %.cpp
17-
@echo Compiling $<...
18-
@$(GPP) -std=c++11 $(GCCFLAGS) -c $< -o $@
19-
20-
$(EXE).elf: $(OBJS)
21-
+$(LD) $^ -o $@ $(GCCFLAGS) $(LDFLAGS)
22-
23-
$(EXE).tns: $(EXE).elf
24-
+$(GENZEHN) --input $^ --output $@.zehn $(ZEHNFLAGS)
25-
+make-prg $@.zehn $@
26-
+rm $@.zehn
27-
28-
.PHONY: clean
29-
clean:
30-
rm -f `find . -name \*.o`
31-
rm -f $(EXE).tns $(EXE).elf
1+
GCC = nspire-gcc
2+
GPP = nspire-g++
3+
LD = nspire-ld
4+
GENZEHN = genzehn
5+
OPTIMIZE ?= fast
6+
GCCFLAGS = -g -O$(OPTIMIZE) -I nGL -I . -Wall -W -marm -ffast-math -mcpu=arm926ej-s -fno-math-errno -fomit-frame-pointer -flto -fno-rtti -fgcse-sm -fgcse-las -funsafe-loop-optimizations -fno-fat-lto-objects -frename-registers -fprefetch-loop-arrays -Wold-style-cast -mno-thumb-interwork -ffunction-sections -fdata-sections
7+
LDFLAGS = -g -lm -Wl,--gc-sections
8+
ZEHNFLAGS = --name "Crafti" --version 11 --author "Fabian Vogt" --notice "3D Minecraft" --compress
9+
EXE = crafti
10+
OBJS = $(patsubst %.c, %.o, $(shell find . -name \*.c))
11+
OBJS += $(patsubst %.cpp, %.o, $(shell find . -name \*.cpp))
12+
OBJS += $(patsubst %.S, %.o, $(shell find . -name \*.S))
13+
14+
all: $(EXE).tns
15+
16+
%.o: %.cpp
17+
@echo Compiling $<...
18+
@$(GPP) -std=c++11 $(GCCFLAGS) -c $< -o $@
19+
20+
$(EXE).elf: $(OBJS)
21+
+$(LD) $^ -o $@ $(GCCFLAGS) $(LDFLAGS)
22+
23+
$(EXE).tns: $(EXE).elf
24+
+$(GENZEHN) --input $^ --output $@.zehn $(ZEHNFLAGS)
25+
+make-prg $@.zehn $@
26+
+rm $@.zehn
27+
28+
.PHONY: clean
29+
clean:
30+
rm -f `find . -name \*.o`
31+
rm -f $(EXE).tns $(EXE).elf

Makefile.pc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
GCC = gcc
2+
GPP = g++
3+
LD = g++
4+
OPTIMIZE ?= g
5+
GCCFLAGS = -g -O$(OPTIMIZE) -I nGL -I . -Wall -W -ffast-math -fno-math-errno -fomit-frame-pointer -flto -fno-rtti -fgcse-sm -fgcse-las -funsafe-loop-optimizations -fno-fat-lto-objects -frename-registers -fprefetch-loop-arrays -Wold-style-cast -ffunction-sections -fdata-sections
6+
LDFLAGS = -lm -lSDL -Wl,--gc-sections
7+
EXE = crafti
8+
OBJS = $(patsubst %.c, %.o, $(shell find . -name \*.c))
9+
OBJS += $(patsubst %.cpp, %.o, $(shell find . -name \*.cpp))
10+
OBJS += $(patsubst %.S, %.o, $(shell find . -name \*.S))
11+
12+
all: $(EXE).elf
13+
14+
%.o: %.cpp
15+
@echo Compiling $<...
16+
@$(GPP) -std=c++14 $(GCCFLAGS) -c $< -o $@
17+
18+
$(EXE).elf: $(OBJS)
19+
+$(LD) $^ -o $@ $(GCCFLAGS) $(LDFLAGS)
20+
21+
.PHONY: clean
22+
clean:
23+
rm -f `find . -name \*.o`
24+
rm -f $(EXE).tns $(EXE).elf

aabb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AABB
1717
NONE
1818
};
1919

20-
AABB() : AABB(0, 0, 0, 0, 0, 0) {};
20+
AABB() : AABB(0, 0, 0, 0, 0, 0) {}
2121
AABB(VERTEX *v1, VERTEX *v2);
2222
AABB(GLFix low_x, GLFix low_y, GLFix low_z, GLFix high_x, GLFix high_y, GLFix high_z);
2323
AABB(VERTEX *list, unsigned int size);

blockrenderer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class BlockRenderer
1313
{
1414
public:
15-
virtual ~BlockRenderer() {};
15+
virtual ~BlockRenderer() {}
1616

1717
virtual void renderSpecialBlock(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z, Chunk &c) = 0;
1818
virtual void geometryNormalBlock(const BLOCK_WDATA block, const int x, const int y, const int z, const BLOCK_SIDE side, Chunk &c) = 0;
@@ -75,8 +75,8 @@ class UniversalBlockRenderer : public BlockRenderer
7575

7676
class DumbBlockRenderer : public BlockRenderer
7777
{
78-
virtual void renderSpecialBlock(const BLOCK_WDATA /*block*/, GLFix /*x*/, GLFix /*y*/, GLFix /*z*/, Chunk &/*c*/) override {};
79-
virtual void geometryNormalBlock(const BLOCK_WDATA /*block*/, const int /*x*/, const int /*y*/, const int /*z*/, const BLOCK_SIDE /*side*/, Chunk &/*c*/) override {};
78+
virtual void renderSpecialBlock(const BLOCK_WDATA /*block*/, GLFix /*x*/, GLFix /*y*/, GLFix /*z*/, Chunk &/*c*/) override {}
79+
virtual void geometryNormalBlock(const BLOCK_WDATA /*block*/, const int /*x*/, const int /*y*/, const int /*z*/, const BLOCK_SIDE /*side*/, Chunk &/*c*/) override {}
8080
virtual bool isOpaque(const BLOCK_WDATA /*block*/) override { return true; }
8181
virtual bool isObstacle(const BLOCK_WDATA /*block*/) override { return false; }
8282
virtual bool isOriented(const BLOCK_WDATA /*block*/) override { return false; }

chunk.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int Chunk::getPosition(int x, int y, int z)
3232
if(pos_indices[x][y][z] == -1)
3333
{
3434
pos_indices[x][y][z] = positions.size();
35-
positions.emplace_back(Position{abs_x + x*BLOCK_SIZE, abs_y + y*BLOCK_SIZE, abs_z + z*BLOCK_SIZE});
35+
positions.emplace_back(VECTOR3{abs_x + x*BLOCK_SIZE, abs_y + y*BLOCK_SIZE, abs_z + z*BLOCK_SIZE});
3636
}
3737

3838
return pos_indices[x][y][z];
@@ -165,9 +165,9 @@ void Chunk::buildGeometry()
165165

166166
#define MAKE_VERTEX(pos, iver) { (pos).x, (pos).y, (pos).z, iver.u, iver.v, iver.c }
167167

168-
VERTEX Chunk::perspective(const IndexedVertex &v, Position &transformed)
168+
VERTEX Chunk::perspective(const IndexedVertex &v, VECTOR3 &transformed)
169169
{
170-
std::pair<Position, bool> &p = positions_perspective[v.pos];
170+
std::pair<VECTOR3, bool> &p = positions_perspective[v.pos];
171171
if(!p.second)
172172
{
173173
VERTEX ver = MAKE_VERTEX(transformed, v);
@@ -183,7 +183,7 @@ VERTEX Chunk::perspective(const IndexedVertex &v, Position &transformed)
183183

184184
bool Chunk::drawTriangle(const IndexedVertex &low, const IndexedVertex &middle, const IndexedVertex &high, bool backface_culling)
185185
{
186-
Position pos_low = positions_transformed[low.pos], pos_middle = positions_transformed[middle.pos], pos_high = positions_transformed[high.pos];
186+
VECTOR3 pos_low = positions_transformed[low.pos], pos_middle = positions_transformed[middle.pos], pos_high = positions_transformed[high.pos];
187187

188188
#ifndef Z_CLIPPING
189189
if(pos_low.z < GLFix(CLIP_PLANE) || pos_middle.z < GLFix(CLIP_PLANE) || pos_high.z < GLFix(CLIP_PLANE))
@@ -201,7 +201,7 @@ bool Chunk::drawTriangle(const IndexedVertex &low, const IndexedVertex &middle,
201201

202202
VERTEX invisible[3];
203203
IndexedVertex visible[3];
204-
Position *pos_visible[3];
204+
VECTOR3 *pos_visible[3];
205205
int count_invisible = -1, count_visible = -1;
206206

207207
if(pos_low.z < GLFix(CLIP_PLANE))
@@ -374,15 +374,10 @@ void Chunk::render()
374374
&& v13.y >= SCREEN_HEIGHT && v14.y >= SCREEN_HEIGHT && v15.y >= SCREEN_HEIGHT && v16.y >= SCREEN_HEIGHT)
375375
return;
376376

377-
std::fill(positions_perspective.begin(), positions_perspective.end(), std::make_pair<Position, bool>({0, 0, 0}, false));
377+
std::fill(positions_perspective.begin(), positions_perspective.end(), std::make_pair<VECTOR3, bool>({0, 0, 0}, false));
378378

379379
for(unsigned int i = 0; i < positions.size(); i++)
380-
{
381-
Position &position = positions[i];
382-
VERTEX t1{position.x, position.y, position.z, 0, 0, 0}, t2;
383-
nglMultMatVectRes(transformation, &t1, &t2);
384-
positions_transformed[i] = {t2.x, t2.y, t2.z};
385-
}
380+
nglMultMatVectRes(transformation, &positions[i], &positions_transformed[i]);
386381

387382
nglForceColor(true);
388383
const IndexedVertex *v = vertices_color.data();
@@ -416,9 +411,9 @@ void Chunk::render()
416411
const VERTEX *ve = vertices_unaligned.data();
417412
for(unsigned int i = 0; i < vertices_unaligned.size(); i += 4, ve += 4)
418413
{
419-
nglMultMatVectRes(transformation, ve + 0, &v1);
420-
nglMultMatVectRes(transformation, ve + 1, &v2);
421-
nglMultMatVectRes(transformation, ve + 2, &v3);
414+
nglMultMatVectRes(transformation, &ve[0], &v1);
415+
nglMultMatVectRes(transformation, &ve[1], &v2);
416+
nglMultMatVectRes(transformation, &ve[2], &v3);
422417

423418
//nglMultMatVectRes doesn't copy u,v and c
424419
v1.u = ve[0].u;
@@ -433,7 +428,7 @@ void Chunk::render()
433428

434429
if(nglDrawTriangle(&v1, &v2, &v3, (v1.c & TEXTURE_DRAW_BACKFACE) == 0))
435430
{
436-
nglMultMatVectRes(transformation, ve + 3, &v4);
431+
nglMultMatVectRes(transformation, &ve[3], &v4);
437432
v4.u = ve[3].u;
438433
v4.v = ve[3].v;
439434
v4.c = ve[3].c;
@@ -557,7 +552,7 @@ bool Chunk::intersects(AABB &other)
557552
return false;
558553
}
559554

560-
bool Chunk::intersectsRay(GLFix rx, GLFix ry, GLFix rz, GLFix dx, GLFix dy, GLFix dz, GLFix &dist, Position &pos, AABB::SIDE &side, bool ignore_water)
555+
bool Chunk::intersectsRay(GLFix rx, GLFix ry, GLFix rz, GLFix dx, GLFix dy, GLFix dz, GLFix &dist, VECTOR3 &pos, AABB::SIDE &side, bool ignore_water)
561556
{
562557
GLFix shortest_dist;
563558
if(aabb.intersectsRay(rx, ry, rz, dx, dy, dz, shortest_dist) == AABB::NONE)
@@ -787,9 +782,13 @@ void drawLoadingtext(const int i)
787782

788783
shown = true;
789784

790-
TEXTURE screen;
791-
screen.width = SCREEN_WIDTH;
792-
screen.height = SCREEN_HEIGHT;
793-
screen.bitmap = reinterpret_cast<COLOR*>(SCREEN_BASE_ADDRESS);
794-
drawTexture(loadingtext, screen, 0, 0, loadingtext.width, loadingtext.height, (SCREEN_WIDTH - loadingtext.width) / 2, 0, loadingtext.width, loadingtext.height);
785+
if(lcd_type() != SCR_320x240_565 && lcd_type() != SCR_320x240_4)
786+
return;
787+
#ifdef _TINSPIRE
788+
TEXTURE screen;
789+
screen.width = SCREEN_WIDTH;
790+
screen.height = SCREEN_HEIGHT;
791+
screen.bitmap = reinterpret_cast<COLOR*>(REAL_SCREEN_BASE_ADDRESS);
792+
drawTexture(loadingtext, screen, 0, 0, loadingtext.width, loadingtext.height, (SCREEN_WIDTH - loadingtext.width) / 2, 0, loadingtext.width, loadingtext.height);
793+
#endif
795794
}

chunk.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
class World;
1313

14-
struct Position {
15-
GLFix x, y, z;
16-
};
17-
1814
struct IndexedVertex {
1915
int pos;
2016
GLFix u, v;
@@ -37,7 +33,7 @@ class Chunk
3733
void setGlobalBlockRelative(const int x, const int y, const int z, const BLOCK_WDATA block, bool set_dirty = true);
3834
AABB &getAABB() { return aabb; }
3935
bool intersects(AABB &other);
40-
bool intersectsRay(GLFix x, GLFix y, GLFix z, GLFix dx, GLFix dy, GLFix dz, GLFix &dist, Position &pos, AABB::SIDE &side, bool ignore_water);
36+
bool intersectsRay(GLFix x, GLFix y, GLFix z, GLFix dx, GLFix dy, GLFix dz, GLFix &dist, VECTOR3 &pos, AABB::SIDE &side, bool ignore_water);
4137
void generate();
4238
bool saveToFile(FILE *file);
4339
bool loadFromFile(FILE *file);
@@ -80,7 +76,7 @@ class Chunk
8076
//Rendering
8177
void geometrySpecialBlock(BLOCK_WDATA block, unsigned int x, unsigned int y, unsigned int z, BLOCK_SIDE side);
8278
void buildGeometry();
83-
VERTEX perspective(const IndexedVertex &v, Position &transformed);
79+
VERTEX perspective(const IndexedVertex &v, VECTOR3 &transformed);
8480
bool drawTriangle(const IndexedVertex &low, const IndexedVertex &middle, const IndexedVertex &high, bool backface_culling = true);
8581

8682
//Data
@@ -92,8 +88,8 @@ class Chunk
9288
bool render_dirty = true;
9389
int pos_indices[SIZE + 1][SIZE + 1][SIZE + 1];
9490
BLOCK_SIDE_BITFIELD sides_rendered[SIZE][SIZE][SIZE] = {}; //It could be that other chunks already rendered parts of our blocks
95-
std::vector<Position> positions, positions_transformed;
96-
std::vector<std::pair<Position, bool>> positions_perspective;
91+
std::vector<VECTOR3> positions, positions_transformed;
92+
std::vector<std::pair<VECTOR3, bool>> positions_perspective;
9793
std::vector<IndexedVertex> vertices, vertices_quad, vertices_color;
9894
std::vector<VERTEX> vertices_unaligned; //The optimized drawing with indices doesn't work with unaligned positions
9995
int tick_counter = 1; //1 to trigger a tick the next frame

0 commit comments

Comments
 (0)