Skip to content

Commit ca1acdb

Browse files
committed
Merge branch 'p8sciiCtrl'
2 parents 21a2149 + a3f7285 commit ca1acdb

15 files changed

Lines changed: 1070 additions & 275 deletions

source/PicoRam.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,14 @@ struct hwState_t {
218218
uint8_t mapMemMapping;
219219
//0x5f57
220220
uint8_t widthOfTheMap;
221-
//0x5f58..0x5f5b
222-
uint8_t printAttributes[4];
221+
//0x5f58
222+
uint8_t printAttributes;
223+
//0x5f59
224+
uint8_t printCharDimensions;
225+
//0x5f5a
226+
uint8_t printTabWidth;
227+
//0x5f5b
228+
uint8_t printOffsetDimensions;
223229
//0x5f5c
224230
uint8_t btnpRepeatDelay;
225231
//0x5f5d

source/graphics.cpp

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ void Graphics::copyStretchSpriteToScreen(
204204
int scr_w,
205205
int scr_h,
206206
bool flip_x,
207-
bool flip_y)
207+
bool flip_y,
208+
//skipStretchPx is currently only used for drawing stripey mode text,
209+
//so it is only used when drawing in non-flipped mode
210+
bool skipStretchPx)
208211
{
209212
if (scr_w == 0 || scr_h == 0)
210213
return;
@@ -287,18 +290,31 @@ void Graphics::copyStretchSpriteToScreen(
287290
dy = -dy;
288291
}
289292

293+
int prevSprX = -1;
294+
int prevSprY = -1;
295+
290296
//ugly duplication but see if inlining helps
291297
if (hwState.colorBitmask == 0xff){
292298
for (int y = 0; y < scr_h; y++) {
293-
uint8_t* spr = spritebuffer + (((spr_y + y * dy) >> 16) & 0x7f) * 64;
299+
int sprY = ((spr_y + y * dy) >> 16);
300+
uint8_t* spr = spritebuffer + (sprY & 0x7f) * 64;
301+
302+
if (skipStretchPx && prevSprY == sprY){
303+
continue;
304+
}
305+
prevSprY = sprY;
294306

295307
if (!flip_x) {
296308
for (int x = 0; x < scr_w; x++) {
297-
int pixIndex = (spr_x + x * dx);
298-
int combinedPixIdx = ((pixIndex / 2) >> 16) & 0x7f;
299-
uint8_t bothPix = spr[combinedPixIdx];
309+
int shiftedPixIndex = (spr_x + x * dx) >> 16;
310+
if (skipStretchPx && prevSprX == shiftedPixIndex){
311+
continue;
312+
}
313+
prevSprX = shiftedPixIndex;
314+
int preShiftedCombinedPixIndex = (shiftedPixIndex / 2) & 0x7f;
315+
uint8_t bothPix = spr[preShiftedCombinedPixIndex];
300316

301-
uint8_t c = (pixIndex >> 16) % 2 == 0
317+
uint8_t c = shiftedPixIndex % 2 == 0
302318
? bothPix & 0x0f //just first 4 bits
303319
: bothPix >> 4; //just last 4 bits
304320

@@ -1247,61 +1263,49 @@ fix32 Graphics::fillp(fix32 pat) {
12471263
return z8::fix32::frombits(prev);
12481264
}
12491265

1250-
int Graphics::print(std::string str) {
1251-
int result = this->print(str, _memory->drawState.text_x, _memory->drawState.text_y);
1252-
1253-
return result;
1254-
}
1255-
1256-
int Graphics::print(std::string str, int x, int y) {
1257-
return this->print(str, x, y, _memory->drawState.color);
1258-
}
12591266

1260-
//based on tac08 impl
1261-
int Graphics::print(std::string str, int x, int y, uint8_t c) {
1262-
color(c);
1267+
int Graphics::drawCharacter(uint8_t ch, int x, int y, uint8_t printMode) {
1268+
int extraCharWidth = 0;
1269+
if ((printMode & PRINT_MODE_ON) == PRINT_MODE_ON){
1270+
int scrW = 4;
1271+
int scrH = 5;
1272+
bool evenPxOnly = false;
12631273

1264-
_memory->drawState.text_x = x;
1265-
_memory->drawState.text_y = y;
1274+
if ((printMode & PRINT_MODE_WIDE) == PRINT_MODE_WIDE) {
1275+
scrW *= 2;
1276+
}
1277+
if((printMode & PRINT_MODE_TALL) == PRINT_MODE_TALL) {
1278+
scrH *= 2;
1279+
}
1280+
if((printMode & PRINT_MODE_STRIPEY) == PRINT_MODE_STRIPEY) {
1281+
//draw every other pixel-- also kinda broken on pico 8 0.2.4
1282+
evenPxOnly = true;
1283+
}
1284+
//TODO: other print modes
12661285

1267-
uint8_t effectiveC = getDrawPalMappedColor(c);
1286+
if (ch >= 0x10 && ch < 0x80) {
1287+
int index = ch - 0x10;
1288+
copyStretchSpriteToScreen(fontSpriteData, (index % 16) * 8, (index / 16) * 8, 4, 5, x, y, scrW, scrH, false, false, evenPxOnly);
1289+
} else if (ch >= 0x80) {
1290+
int index = ch - 0x80;
1291+
extraCharWidth = 4;
1292+
copyStretchSpriteToScreen(fontSpriteData, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5, x, y, (scrW + extraCharWidth), scrH, false, false, evenPxOnly);
1293+
1294+
}
12681295

1269-
//font sprite sheet has text as color 7, with 0 as transparent. We need to override
1270-
//these values and restore them after
1271-
uint8_t prevDrawPal[16];
1272-
for(uint8_t i = 0; i < 16; i++) {
1273-
prevDrawPal[i] = _memory->drawState.drawPaletteMap[i];
1274-
_memory->drawState.drawPaletteMap[i] = i;
12751296
}
1276-
1277-
_memory->drawState.drawPaletteMap[7] = effectiveC;
1278-
_memory->drawState.drawPaletteMap[0] = 16; //transparent
1279-
1280-
1281-
for (size_t n = 0; n < str.length(); n++) {
1282-
uint8_t ch = str[n];
1297+
else{
12831298
if (ch >= 0x10 && ch < 0x80) {
12841299
int index = ch - 0x10;
12851300
copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8, 4, 5, false, false);
1286-
x += 4;
12871301
} else if (ch >= 0x80) {
12881302
int index = ch - 0x80;
12891303
copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5, false, false);
1290-
x += 8;
1291-
} else if (ch == '\n') {
1292-
x = _memory->drawState.text_x;
1293-
y += 6;
1304+
extraCharWidth = 4;
12941305
}
12951306
}
12961307

1297-
for(int i = 0; i < 16; i++) {
1298-
_memory->drawState.drawPaletteMap[i] = prevDrawPal[i];
1299-
}
1300-
1301-
//todo: auto scrolling
1302-
_memory->drawState.text_y += 6;
1303-
1304-
return x;
1308+
return extraCharWidth;
13051309
}
13061310

13071311
void Graphics::spr(
@@ -1332,7 +1336,7 @@ void Graphics::sspr(
13321336
bool flip_x = false,
13331337
bool flip_y = false)
13341338
{
1335-
copyStretchSpriteToScreen(GetP8SpriteSheetBuffer(), sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y);
1339+
copyStretchSpriteToScreen(GetP8SpriteSheetBuffer(), sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y, false);
13361340
}
13371341

13381342
bool Graphics::fget(uint8_t n, uint8_t f){

source/graphics.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class Graphics {
3535
int scr_w,
3636
int scr_h,
3737
bool flip_x,
38-
bool flip_y);
38+
bool flip_y,
39+
bool skipStretchPx);
3940

4041
void swap(int *x, int *y);
4142
void applyCameraToPoint(int *x, int *y);
@@ -108,9 +109,7 @@ class Graphics {
108109

109110
fix32 fillp(fix32 pat);
110111

111-
int print(std::string str);
112-
int print(std::string str, int x, int y);
113-
int print(std::string str, int x, int y, uint8_t c);
112+
int drawCharacter(uint8_t ch, int x, int y, uint8_t printMode = 0);
114113

115114
void spr(
116115
int n,

source/hostVmShared.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <stdint.h>
4+
35
/// Creates a bitmask from a bit number.
46
#define BITMASK(n) (1U<<(n))
57

@@ -71,3 +73,15 @@ struct InputState_t {
7173
int16_t mouseY;
7274
uint8_t mouseBtnState;
7375
};
76+
77+
enum PrintMode_t
78+
{
79+
PRINT_MODE_ON = 0x1,
80+
PRINT_MODE_PADDING = 0x2,
81+
PRINT_MODE_WIDE = 0x4,
82+
PRINT_MODE_TALL = 0x8,
83+
PRINT_MODE_SOLID_BG = 0x10,
84+
PRINT_MODE_INVERTED = 0x20,
85+
PRINT_MODE_STRIPEY = 0x40,
86+
PRINT_MODE_CUSTOM_FONT = 0x80
87+
};

source/picoluaapi.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using namespace std;
99
#include "Input.h"
1010
#include "vm.h"
1111
#include "logger.h"
12+
#include "printHelper.h"
1213

1314
//extern "C" {
1415
#include <lua.h>
@@ -21,11 +22,13 @@ Input* _inputForLuaApi;
2122
Vm* _vmForLuaApi;
2223
Audio* _audioForLuaApi;
2324

24-
void initPicoApi(Graphics* graphics, Input* input, Vm* vm, Audio* audio){
25+
void initPicoApi(PicoRam* memory, Graphics* graphics, Input* input, Vm* vm, Audio* audio){
2526
_graphicsForLuaApi = graphics;
2627
_inputForLuaApi = input;
2728
_vmForLuaApi = vm;
2829
_audioForLuaApi = audio;
30+
31+
initPrintHelper(memory, _graphicsForLuaApi, _vmForLuaApi, _audioForLuaApi);
2932
}
3033

3134
int noop(const char * name) {
@@ -318,27 +321,27 @@ int print(lua_State *L){
318321
}
319322

320323
if (numArgs < 2) {
321-
newx = _graphicsForLuaApi->print(str);
324+
newx = print(str);
322325
}
323326
else if (numArgs == 2) {
324327
uint8_t c = lua_tonumber(L,2);
325328

326329
_graphicsForLuaApi->color(c);
327-
newx = _graphicsForLuaApi->print(str);
330+
newx = print(str);
328331
}
329332
else if (numArgs == 3) {
330333
int x = lua_tonumber(L,2);
331334
int y = lua_tonumber(L,3);
332335

333-
newx = _graphicsForLuaApi->print(str, x, y);
336+
newx = print(str, x, y);
334337
}
335338
else {
336339
int x = lua_tonumber(L,2);
337340
int y = lua_tonumber(L,3);
338341

339342
uint8_t c = lua_tonumber(L,4);
340343

341-
newx = _graphicsForLuaApi->print(str, x, y, c);
344+
newx = print(str, x, y, c);
342345
}
343346

344347
lua_pushinteger(L, newx);

source/picoluaapi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
#include "graphics.h"
1212
#include "Input.h"
1313
#include "vm.h"
14+
#include "PicoRam.h"
1415

1516
//this can probably go away when I'm loading actual carts and just have to expose api to lua
16-
void initPicoApi(Graphics* graphics, Input* input, Vm* vm, Audio* audio);
17+
void initPicoApi(PicoRam* memory, Graphics* graphics, Input* input, Vm* vm, Audio* audio);
1718

1819
//graphics api
1920
int cls(lua_State *L);

0 commit comments

Comments
 (0)