Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap UG_FONT struct byte array access with macro in case an accessor function is needed #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CC=gcc
CFLAGS=-g -std=c99 `sdl2-config --libs --cflags`

all:
${CC} ${CFLAGS} -o ugui_sim sdl.c ../ugui.c -I../ phone.c
71 changes: 71 additions & 0 deletions examples/phone.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "ugui.h"
#define D_FONT FONT_10X16
#define MAX_OBJECTS 100


UG_WINDOW window1;
UG_TEXTBOX textbox1;
UG_BUTTON numpad[12];
static char *glyphs[12] = {"1", "2", "3",
"4", "5", "6",
"7", "8", "9",
"X", "0", "C"};
UG_OBJECT objs[MAX_OBJECTS];
static char number[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int np;

void window1_callback(UG_MESSAGE *msg){
if (msg->type == MSG_TYPE_OBJECT){
if(msg->id == OBJ_TYPE_BUTTON){
if (msg->event == OBJ_EVENT_PRESSED || msg->event == OBJ_EVENT_CLICKED){
if ((msg->sub_id >= 0 && msg->sub_id <= 8) || msg->sub_id == 10){
if (np < sizeof(number)-1){
number[np++] = glyphs[msg->sub_id][0];
UG_TextboxSetText(&window1, TXB_ID_0, number);
}
}
else {
if (msg->sub_id == 11)
UG_TextboxSetText(&window1, TXB_ID_0, "Calling..");
else if (msg->sub_id == 9){
number[--np] = 0x00;
UG_TextboxSetText(&window1, TXB_ID_0, number);
}
}
}
}
}
}

void ui_setup(){

UG_FontSelect(&D_FONT);
UG_WindowCreate(&window1, objs, MAX_OBJECTS, window1_callback);

UG_WindowSetTitleText(&window1, "Caller UI");
UG_WindowSetTitleTextFont(&window1, &D_FONT);

UG_TextboxCreate(&window1, &textbox1, TXB_ID_0, 10, 10, 230, 34);
UG_TextboxSetFont(&window1, TXB_ID_0, &D_FONT);
UG_TextboxSetText(&window1, TXB_ID_0, "enter number");
UG_TextboxSetForeColor(&window1, TXB_ID_0, C_BLACK);

for (int y = 0; y < 4; y++){
for (int x = 0; x < 3; x++){
int i = y*3 + x;
#define BTN_SIZE 55
UG_ButtonCreate(&window1, &numpad[i], BTN_ID_0+i, 40+((x)*BTN_SIZE), 20+30+((y)*BTN_SIZE), 85+((x)*BTN_SIZE), 50+30+15+((y)*BTN_SIZE));
UG_ButtonSetStyle(&window1, BTN_ID_0+i, BTN_STYLE_2D);
UG_ButtonSetFont(&window1, BTN_ID_0+i, &D_FONT);
UG_ButtonSetText(&window1, BTN_ID_0+i, glyphs[i]);
UG_ButtonSetBackColor(&window1, BTN_ID_0+i, C_BLACK);
UG_ButtonSetForeColor(&window1, BTN_ID_0+i, C_WHITE);
}
}

UG_WindowShow(&window1);
}

void ui_loop(){

}
67 changes: 67 additions & 0 deletions examples/sdl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <stdio.h>
#include <SDL2/SDL.h>
#include "ugui.h"

#define DISPLAY_COLS 240
#define DISPLAY_ROWS 340

uint32_t display[DISPLAY_COLS*DISPLAY_ROWS];

extern void ui_setup(void);
extern void ui_loop(void);

void PixelFunc(UG_S16 x, UG_S16 y, UG_COLOR c){
display[(y*DISPLAY_COLS)+x] = (c<<8);
}

int main(int argc, char **argv){

UG_GUI gui;

UG_Init(&gui, PixelFunc, DISPLAY_COLS, DISPLAY_ROWS);
UG_SelectGUI(&gui);

if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}

SDL_Window * window = SDL_CreateWindow("uGUI simulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DISPLAY_COLS, DISPLAY_ROWS, 0);
SDL_Renderer * render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Texture * texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, DISPLAY_COLS, DISPLAY_ROWS);

memset(display, 0x00, sizeof(uint32_t)*DISPLAY_COLS*DISPLAY_ROWS);

ui_setup();

SDL_UpdateTexture(texture, NULL, display, DISPLAY_COLS*sizeof(uint32_t));
SDL_RenderCopy(render, texture, NULL, NULL);
SDL_RenderPresent(render);

while (1){

SDL_Event event;

for (; SDL_PollEvent(&event); ){
switch(event.type){
case SDL_MOUSEBUTTONDOWN:
UG_TouchUpdate(event.button.x, event.button.y, TOUCH_STATE_PRESSED);
break;
case SDL_MOUSEBUTTONUP:
UG_TouchUpdate(-1, -1, TOUCH_STATE_RELEASED);
break;
case SDL_QUIT:
return 0;
default:
break;
}
}

ui_loop();
UG_Update();
SDL_UpdateTexture(texture, NULL, display, DISPLAY_COLS*sizeof(uint32_t));
SDL_RenderCopy(render, texture, NULL, NULL);
SDL_RenderPresent(render);
SDL_Delay(40);
}
}
8 changes: 4 additions & 4 deletions ugui.c
Original file line number Diff line number Diff line change
Expand Up @@ -5298,7 +5298,7 @@ void _UG_PutChar( char chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc, const
c=actual_char_width;
for( i=0;i<bn;i++ )
{
b = font->p[index++];
b = FONT_DATA_ACCESS(font->p[index++]);
for( k=0;(k<8) && c;k++ )
{
if( b & 0x01 )
Expand All @@ -5322,7 +5322,7 @@ void _UG_PutChar( char chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc, const
{
for( i=0;i<actual_char_width;i++ )
{
b = font->p[index++];
b = FONT_DATA_ACCESS(font->p[index++]);
color = (((fc & 0xFF) * b + (bc & 0xFF) * (256 - b)) >> 8) & 0xFF |//Blue component
(((fc & 0xFF00) * b + (bc & 0xFF00) * (256 - b)) >> 8) & 0xFF00|//Green component
(((fc & 0xFF0000) * b + (bc & 0xFF0000) * (256 - b)) >> 8) & 0xFF0000; //Red component
Expand All @@ -5344,7 +5344,7 @@ void _UG_PutChar( char chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc, const
c=actual_char_width;
for( i=0;i<bn;i++ )
{
b = font->p[index++];
b = FONT_DATA_ACCESS(font->p[index++]);
for( k=0;(k<8) && c;k++ )
{
if( b & 0x01 )
Expand All @@ -5371,7 +5371,7 @@ void _UG_PutChar( char chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc, const
xo = x;
for( i=0;i<actual_char_width;i++ )
{
b = font->p[index++];
b = FONT_DATA_ACCESS(font->p[index++]);
color = (((fc & 0xFF) * b + (bc & 0xFF) * (256 - b)) >> 8) & 0xFF |//Blue component
(((fc & 0xFF00) * b + (bc & 0xFF00) * (256 - b)) >> 8) & 0xFF00|//Green component
(((fc & 0xFF0000) * b + (bc & 0xFF0000) * (256 - b)) >> 8) & 0xFF0000; //Red component
Expand Down
3 changes: 3 additions & 0 deletions ugui_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
/* Specify platform-dependent integer types here */

#define __UG_FONT_DATA const
/* Override if you store font data not in RAM/SRAM
* for example for pgm_read_byte w/ PROGMEM on AVR */
#define FONT_DATA_ACCESS(x) (x)
typedef uint8_t UG_U8;
typedef int8_t UG_S8;
typedef uint16_t UG_U16;
Expand Down