Skip to content

Commit

Permalink
Merge pull request #451 from paulscottrobson/cursors
Browse files Browse the repository at this point in the history
Added New Cursor graphics
  • Loading branch information
paulscottrobson authored Apr 13, 2024
2 parents a45a8c6 + 61749a9 commit a2ac4de
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 37 deletions.
1 change: 1 addition & 0 deletions basic/test.bsc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
' BASIC Mouse cursor manipulation
'
mouse show
cls:line 0,0 ink 3 to 100,100
mouse to 260,180
repeat
b = mouse(x,y,s)
Expand Down
5 changes: 4 additions & 1 deletion emulator/src/core/sys_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ void DBGXRender(int *address,int showDisplay) {
}
const uint8_t *cursorImage;
uint16_t cursorX,cursorY;
if (MSEGetCursorDrawInformation(&cursorImage,&cursorX,&cursorY)) {
uint8_t xHit,yHit;
if (MSEGetCursorDrawInformation(&cursorX,&cursorY)) {
cursorImage = CURGetCurrent(&xHit,&yHit);
cursorX -= xHit;cursorY -= yHit;
uint8_t w = 16,h = 16;
if (cursorX + 16 >= 320) w = 320-cursorX;
if (cursorY + 16 >= 240) h = 240-cursorY;
Expand Down
1 change: 1 addition & 0 deletions firmware/common/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "interface/graphics.h"
#include "interface/console.h"
#include "interface/mouse.h"
#include "interface/cursor.h"
#include "interface/timer.h"
#include "interface/sound.h"
#include "interface/memory.h"
Expand Down
28 changes: 28 additions & 0 deletions firmware/common/include/interface/cursor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ***************************************************************************************
// ***************************************************************************************
//
// Name : cursor.h
// Authors : Paul Robson ([email protected])
// Date : 13th April 2024
// Reviewed : No
// Purpose : Cursor header
//
// ***************************************************************************************
// ***************************************************************************************

#ifndef _CURSOR_H
#define _CURSOR_H

void CURInitialise(void);
const uint8_t *CURGetCurrent(uint8_t *xHit,uint8_t *yHit);
bool CURSetCurrent(uint8_t cursorID);


#endif

// ***************************************************************************************
//
// Date Revision
// ==== ========
//
// ***************************************************************************************
6 changes: 3 additions & 3 deletions firmware/common/include/interface/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
// ***************************************************************************************
// ***************************************************************************************

#ifndef _CURSOR_H
#define _CURSOR_H
#ifndef _MOUSE_H
#define _MOUSE_H

void MSEInitialise(void);
void MSESetPosition(uint16_t x, uint16_t y);
void MSEOffsetPosition(int8_t dx, int8_t dy);
void MSEUpdateScrollWheel(int8_t ds);
void MSEUpdateButtonState(uint8_t buttonState);
void MSEGetState(uint16_t *pX, uint16_t *pY, uint8_t *pButtonState, uint8_t *pScrollWheelState);
bool MSEGetCursorDrawInformation(const uint8_t **pData, uint16_t *pX, uint16_t *pY);
bool MSEGetCursorDrawInformation(uint16_t *pX, uint16_t *pY);
void MSESetVisible(bool isVisible);
void MSEEnableMouse(void);
bool MSEMousePresent(void);
Expand Down
59 changes: 36 additions & 23 deletions firmware/common/scripts/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,45 @@
#
# Name : cursors.py
# Authors : Paul Robson ([email protected])
# Date : 21st March 2024
# Date : 13th April 2024
# Reviewed : No
# Purpose : Converts cursors to data (monochrome only at present)
# Purpose : Converts cursors to data (2nd version)
#
# ***************************************************************************************
# ***************************************************************************************

import re,os,sys
from PIL import Image


class Cursor(object):
def __init__(self,imageFile,xClick,yClick):
self.click = (xClick,yClick)
self.image = Image.open("scripts"+os.sep+"cursors"+os.sep+imageFile)
assert self.image.size == (16,16)
self.imageData = [0xFF] * 256
self.name = imageFile[:-4].lower()
for x in range(0,16):
for y in range(0,16):
p = self.image.getpixel((x,y))
if p[3] > 128:
self.imageData[x+y*16] = 7 if p[0]+p[1]+p[2] > 512 else 0
ib = ",".join([str(x) for x in self.imageData])
print("static const uint8_t {1}_cursor_data[] = {{ {0}}};\n".format(ib,self.name))

cursors = []
print("//\n//\tThis file is automatically generated.\n//")
Cursor("default.png",0,0)

def convert(n):
n = n & 0x0F
return 0xFF if n == 0 else n

src = [x for x in open("scripts/cursors/graphics.gfx","rb").read(-1)]
assert src[0] == 1 and src[1] == 0 and src[3] == 0
cursorCount = src[2]

print("//\n//\tThis file is automatically generated\n//\n")

print("#define CURSOR_IMAGE_COUNT ({0})\n".format(cursorCount))

cursorData = []
for c in src[256:]:
cursorData.append(convert(c >> 4))
cursorData.append(convert(c))

print("const uint8_t cursor_data[] = {")
print(",".join([str(x) for x in cursorData]))
print("};\n")

hitPoint = []
for i in range(0,cursorCount):
hp = [8,8]
if i == 0:
hp = [4,0]
if i == 4 or i == 9:
hp = [4,4]
hitPoint += hp
print("const uint8_t cursor_hitPoint[] = {")
print(",".join([str(x) for x in hitPoint]))
print("};\n")

23 changes: 23 additions & 0 deletions firmware/common/scripts/cursors/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ***************************************************************************************
# ***************************************************************************************
#
# Name : Makefile
# Author : Paul Robson ([email protected])
# Date : 20th November 2023
# Reviewed : No
# Purpose : Main firmware makefile, most of the work is done by CMake.
#
# ***************************************************************************************
# ***************************************************************************************

ifeq ($(OS),Windows_NT)
#is this really needed? WSL2 is the 2023.
include ..\..\..\..\build_env\common.make
else
include ../../../../build_env/common.make
endif

all:

$(PYTHON) $(BINDIR)makeimg.zip

Binary file removed firmware/common/scripts/cursors/default.png
Binary file not shown.
Binary file added firmware/common/scripts/cursors/graphics.gfx
Binary file not shown.
Binary file added firmware/common/scripts/cursors/sprite_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/common/scripts/cursors/sprite_32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/common/scripts/cursors/tile_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions firmware/common/sources/interface/cursor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// ***************************************************************************************
// ***************************************************************************************
//
// Name : cursor.cpp
// Authors : Paul Robson ([email protected])
// Date : 13th April 2024
// Reviewed : No
// Purpose : Cursor Repository.
//
// ***************************************************************************************
// ***************************************************************************************

#include "common.h"
#include "data/cursors.h"
static uint8_t currentCursor;

// ***************************************************************************************
//
// Initialise cursors
//
// ***************************************************************************************

void CURInitialise(void) {
currentCursor = 0;
}

// ***************************************************************************************
//
// Get current cursor
//
// ***************************************************************************************

const uint8_t *CURGetCurrent(uint8_t *xHit,uint8_t *yHit) {
if (xHit != NULL) *xHit = cursor_hitPoint[currentCursor*2];
if (yHit != NULL) *yHit = cursor_hitPoint[currentCursor*2+1];
return cursor_data+currentCursor*256;
}

// ***************************************************************************************
//
// Set current cursor
//
// ***************************************************************************************

bool CURSetCurrent(uint8_t cursorID) {
bool isOk = cursorID < CURSOR_IMAGE_COUNT; // Number of cursors
currentCursor = isOk ? cursorID : 0; // Bad value sets default
return !isOk; // Return false on error
}

// ***************************************************************************************
//
// Date Revision
// ==== ========
//
// ***************************************************************************************
1 change: 1 addition & 0 deletions firmware/common/sources/interface/dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void DSPReset(void) {
const char bootString[] = PROMPT;
MEMInitialiseMemory(); // Set up memory, load kernel ROM
MSEInitialise(); // Mouse first, before starting graphics.
CURInitialise();
GFXSetMode(0); // Initialise graphics
SPRReset(); // Reset sprites.
LOGDrawLogo(); // Draw logo
Expand Down
3 changes: 1 addition & 2 deletions firmware/common/sources/interface/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ void MSESetVisible(bool isVisible) {
//
// ***************************************************************************************

bool MSEGetCursorDrawInformation(const uint8_t **pData, uint16_t *pX, uint16_t *pY) {
*pData = default_cursor_data; // Just one cursor
bool MSEGetCursorDrawInformation(uint16_t *pX, uint16_t *pY) {
*pX = xCursor; *pY = yCursor; // This is top left - so crosshairs will need adjusting for example.
return isCursorVisible && xCursor < gMode.xGSize && yCursor < gMode.yGSize; // On and on screen.
}
Expand Down
21 changes: 13 additions & 8 deletions firmware/sources/hardware/dvi_320x240x256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ static void __not_in_flash_func(_scanline_callback)(void) {
if (lineCounter == FRAME_HEIGHT) {
frameCounter++;
lineCounter = 0;
cursorEnabled = MSEGetCursorDrawInformation(&cursorImage, // Get cursor info this frame.
&xCursor,&yCursor);
uint8_t xHit,yHit;
cursorEnabled = MSEGetCursorDrawInformation(&xCursor,&yCursor); // Get cursor info this frame.
cursorImage = CURGetCurrent(&xHit,&yHit);
xCursor -= xHit;yCursor -= yHit;

if (cursorEnabled) { // If enabled work out physical drawing height.
wCursor = hCursor = 16; // Could be partially drawn.
if (xCursor + 16 >= 320) wCursor = 320-xCursor;
Expand All @@ -84,12 +87,14 @@ static void __not_in_flash_func(_scanline_callback)(void) {
*scanline++ = palette[*screenPos++]; // convert using palette => buffer.
}
if (cursorEnabled && lineCounter>=yCursor && lineCounter<yCursor+hCursor) { // Cursor drawing on this line.
const uint8_t *cursorData = cursorImage + (lineCounter-yCursor) * 16;
cursline += xCursor; // Position on this line.
for (uint16_t i = 0;i < wCursor;i++) { // Each pixel.
uint8_t pixel = *cursorData++;
if (pixel != 0xFF) *cursline = palette[pixel]; // Check for transparency
cursline++;
if (xCursor >= 0 && xCursor < 320-16) { // On Screen ?
const uint8_t *cursorData = cursorImage + (lineCounter-yCursor) * 16;
cursline += xCursor; // Position on this line.
for (uint16_t i = 0;i < wCursor;i++) { // Each pixel.
uint8_t pixel = *cursorData++;
if (pixel != 0xFF) *cursline = palette[pixel]; // Check for transparency
cursline++;
}
}
}
}
Expand Down

0 comments on commit a2ac4de

Please sign in to comment.