From a8d54ab31f7a75b02fba42b218edda287aaa1080 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Mon, 8 Sep 2008 21:13:26 +0000 Subject: [PATCH] Start of the new configuration file engine. Still needs some work. See "cfg" folder for the old pascal version. git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@126 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- cfg_new/5pxtinyfont.png | Bin 0 -> 720 bytes cfg_new/Makefile | 11 ++ cfg_new/SFont.c | 178 +++++++++++++++++++++++++ cfg_new/SFont.h | 83 ++++++++++++ cfg_new/gfxcfg.c | 285 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 557 insertions(+) create mode 100644 cfg_new/5pxtinyfont.png create mode 100644 cfg_new/Makefile create mode 100644 cfg_new/SFont.c create mode 100644 cfg_new/SFont.h create mode 100644 cfg_new/gfxcfg.c diff --git a/cfg_new/5pxtinyfont.png b/cfg_new/5pxtinyfont.png new file mode 100644 index 0000000000000000000000000000000000000000..c9f4d9b30010c9354446514b75966c5831ad750c GIT binary patch literal 720 zcmV;>0x$iEP) z`UV`yViol%(5xa(Io}QxRQ2>|7BkYM>4F3&e*XD^KIWc!nyIDst!r?5@0zxucUqdc zLwEMu8!gL%-;SEOB}8M?W0wX)T^n(<;16%Hn7{H>+qlnj-(#fpm&eti_d#c`Z|giy zMt#@$I&^Pl^v@a-si&Z1{=m`uP1Aug*(5YFljTaLBW-Mnm`k$B(0jyB=rjJ46};Rg z6oa;siU^~N3CZ@Ag+*6dIh9cpn-f(oaX_E&4ZqoTa&Z|1OQ447fb~48mY7DYg)Dbb zl}&Zjm62nsScn^wBn*CHo(cySvS&gckFn~-^`e*Eb+scn?P3E;px}zZCPiG)Q z4BrV4;*>pg)GQcqu3WhU5`jSpTmTzTR3@j4j8#nkoluGBj(hadT4LVHs{6%78GE(c zW`M|cabX_^-x=4eV6gCR6H+R;CUY+A<7zv&Hl`s<7MXyJeZ063c5?Z?>M9|mt%}Fv zLUpclw(Qc9Ns_7;(&kkx#Mx0;aB<*a4d~_Kr&@I7y7aO`FK~^}3p_AV<@$6krh|)1 zaFv70E-qL~4P~JMROEVv!&u=kS(9&VHGz|$3W63`1J-kw9eNSN>;?X!XMe%>BmBju zzp!?$%YZe~RK4h{UKCu2$^!2`_K`JhQ2&R;cfyV49C6l|w?uwaw}-#nQYy!J6gtmi zspnCth+rSF1K{!;shnIT{DnD1!(X_a>O?l=Tzt`nIX+Hs%W7B5H^32-+0000 + +#include +#include +#include "SFont.h" + +static Uint32 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y) +{ + Uint8 *bits; + Uint32 Bpp; + + assert(X>=0); + assert(Xw); + + Bpp = Surface->format->BytesPerPixel; + bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp; + + // Get the pixel + switch(Bpp) { + case 1: + return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X); + break; + case 2: + return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X); + break; + case 3: { // Format/endian independent + Uint8 r, g, b; + r = *((bits)+Surface->format->Rshift/8); + g = *((bits)+Surface->format->Gshift/8); + b = *((bits)+Surface->format->Bshift/8); + return SDL_MapRGB(Surface->format, r, g, b); + } + break; + case 4: + return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X); + break; + } + + return -1; +} + +SFont_Font* SFont_InitFont(SDL_Surface* Surface) +{ + int x = 0, i = 0; + Uint32 pixel; + SFont_Font* Font; + Uint32 pink; + + if (Surface == NULL) + return NULL; + + Font = (SFont_Font *) malloc(sizeof(SFont_Font)); + Font->Surface = Surface; + + SDL_LockSurface(Surface); + + pink = SDL_MapRGB(Surface->format, 255, 0, 255); + while (x < Surface->w) { + if (GetPixel(Surface, x, 0) == pink) { + Font->CharPos[i++]=x; + while((x < Surface->w) && (GetPixel(Surface, x, 0)== pink)) + x++; + Font->CharPos[i++]=x; + } + x++; + } + Font->MaxPos = x-1; + + pixel = GetPixel(Surface, 0, Surface->h-1); + SDL_UnlockSurface(Surface); + SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel); + + return Font; +} + +void SFont_FreeFont(SFont_Font* FontInfo) +{ + SDL_FreeSurface(FontInfo->Surface); + free(FontInfo); +} + +void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font, + int x, int y, const char *text) +{ + const char* c; + int charoffset; + SDL_Rect srcrect, dstrect; + + if(text == NULL) + return; + + // these values won't change in the loop + srcrect.y = 1; + dstrect.y = y; + srcrect.h = dstrect.h = Font->Surface->h - 1; + + for(c = text; *c != '\0' && x <= Surface->w ; c++) { + charoffset = ((int) (*c - 33)) * 2 + 1; + // skip spaces and nonprintable characters + if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) { + x += Font->CharPos[2]-Font->CharPos[1]; + continue; + } + + srcrect.w = dstrect.w = + (Font->CharPos[charoffset+2] + Font->CharPos[charoffset+1])/2 - + (Font->CharPos[charoffset] + Font->CharPos[charoffset-1])/2; + srcrect.x = (Font->CharPos[charoffset]+Font->CharPos[charoffset-1])/2; + dstrect.x = x - (float)(Font->CharPos[charoffset] + - Font->CharPos[charoffset-1])/2; + + SDL_BlitSurface(Font->Surface, &srcrect, Surface, &dstrect); + + x += Font->CharPos[charoffset+1] - Font->CharPos[charoffset]; + } +} + +int SFont_TextWidth(const SFont_Font *Font, const char *text) +{ + const char* c; + int charoffset=0; + int width = 0; + + if(text == NULL) + return 0; + + for(c = text; *c != '\0'; c++) { + charoffset = ((int) *c - 33) * 2 + 1; + // skip spaces and nonprintable characters + if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) { + width += Font->CharPos[2]-Font->CharPos[1]; + continue; + } + + width += Font->CharPos[charoffset+1] - Font->CharPos[charoffset]; + } + + return width; +} + +int SFont_TextHeight(const SFont_Font* Font) +{ + return Font->Surface->h - 1; +} + +void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font, + int y, const char *text) +{ + SFont_Write(Surface, Font, Surface->w/2 - SFont_TextWidth(Font, text)/2, + y, text); +} + diff --git a/cfg_new/SFont.h b/cfg_new/SFont.h new file mode 100644 index 00000000..2bcda190 --- /dev/null +++ b/cfg_new/SFont.h @@ -0,0 +1,83 @@ +/* SFont: a simple font-library that uses special bitmaps as fonts + Copyright (C) 2003 Karl Bartel + + License: GPL or LGPL (at your choice) + WWW: http://www.linux-games.com/sfont/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Karl Bartel + Cecilienstr. 14 + 12307 Berlin + GERMANY + karlb@gmx.net +*/ + +/************************************************************************ +* SFONT - SDL Font Library by Karl Bartel * +* * +* All functions are explained below. For further information, take a * +* look at the example files, the links at the SFont web site, or * +* contact me, if you problem isn' addressed anywhere. * +* * +************************************************************************/ +#ifndef SFONT_H +#define SFONT_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// Delcare one variable of this type for each font you are using. +// To load the fonts, load the font image into YourFont->Surface +// and call InitFont( YourFont ); +typedef struct { + SDL_Surface *Surface; + int CharPos[512]; + int MaxPos; +} SFont_Font; + +// Initializes the font +// Font: this contains the suface with the font. +// The Surface must be loaded before calling this function +SFont_Font* SFont_InitFont (SDL_Surface *Font); + +// Frees the font +// Font: The font to free +// The font must be loaded before using this function. +void SFont_FreeFont(SFont_Font* Font); + +// Blits a string to a surface +// Destination: the suface you want to blit to +// text: a string containing the text you want to blit. +void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font, int x, int y, + const char *text); + +// Returns the width of "text" in pixels +int SFont_TextWidth(const SFont_Font* Font, const char *text); +// Returns the height of "text" in pixels (which is always equal to Font->Surface->h) +int SFont_TextHeight(const SFont_Font* Font); + +// Blits a string to Surface with centered x position +void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font* Font, int y, + const char *text); + +#ifdef __cplusplus +} +#endif + +#endif /* SFONT_H */ diff --git a/cfg_new/gfxcfg.c b/cfg_new/gfxcfg.c new file mode 100644 index 00000000..b056d03d --- /dev/null +++ b/cfg_new/gfxcfg.c @@ -0,0 +1,285 @@ +//C +#include + +//POSIX +#include + +//SDL +#include +#include +#include + +//mine +#include "SFont.h" + +/*** Constants ***/ +#define NB_MAX_OPTIONS 134 + +/*** Types definitions and structs ***/ + +typedef struct{ + uint16_t Numero; + uint16_t Touche; + uint16_t Touche2; + char Libelle[36]; + char Explic1[77]; + char Explic2[77]; + bool Erreur; + bool Suppr; +} Options; + +typedef struct{ + char Signature[3]; + uint8_t Version1; + uint8_t Version2; + uint8_t Beta1; + uint8_t Beta2; +} Type_header; + +#define Header_size 7 + +typedef struct{ + uint8_t Numero; + uint16_t Taille; +} Type_chunk; + +#define Chunk_size 3 + +/*** Global variables ***/ +SFont_Font* MyFont; +SDL_Surface* Ecran; +int Numero_definition_option; +Options Config[NB_MAX_OPTIONS]; +bool Choix_enreg; +uint16_t NB_OPTIONS; + +/*** Fonctions de gestion des évènements SDL ***/ + +/* Attend qu'une touche soit pressée. Retourne le keysym. */ +SDL_keysym Lire_Touche(void) +{ + SDL_Event Event; + + do{ + SDL_WaitEvent(&Event); + if(Event.type==SDL_KEYDOWN) break; + }while(1); + + return Event.key.keysym; +} + +/*** Drawing functions ***/ + +/* Draws a filled rectanble */ +void Cadre(int x,int y,int w,int h,uint8_t color) +{ + SDL_Rect rct; + rct.x=x; + rct.y=y; + rct.w=w; + rct.h=h; + + SDL_FillRect(Ecran, &rct, color); +} + +/* Draws the main screen and welcome message */ +void Dessiner_ecran_principal() +{ + Cadre(3,3,630,40,2); + SFont_Write(Ecran, MyFont, 8,6,"Setup program for Grafx2 (c) 1996-98 Sunset Design and 2008 PulkoMandy"); + SFont_Write(Ecran, MyFont, 8,18,"Use Up/Down arrows & Page-Up/Page-Down to scroll, Enter to modify, Delete to remove a hot-key, and Escape to validate or cancel."); + SFont_Write(Ecran, MyFont, 8,30,"DO NOT USE Print-screen, Pause, and other special keys!"); + + Cadre(3,46,630,400,1); + + SDL_UpdateRect(Ecran,0,0,640,480); + +} + +void Tout_ecrire(uint16_t Decalage_curseur,uint16_t Position_curseur) +{ + puts("TOUT ECRIRE UNIMPLEMENTED"); +} + +/*** Configuration handling functions ***/ + +/* Defines an option */ +void Definir_option(uint16_t Numero, char* Libelle, char* Explic1, char* Explic2, bool Deletable, + uint16_t Default_key) +{ + Config[Numero_definition_option].Numero = Numero; + Config[Numero_definition_option].Touche = Default_key; + Config[Numero_definition_option].Touche2 = 0xFF; + strncpy(Config[Numero_definition_option].Libelle,Libelle,36); + strncpy(Config[Numero_definition_option].Explic1,Explic1,77); + strncpy(Config[Numero_definition_option].Explic2,Explic2,77); + Config[Numero_definition_option].Erreur = false; + Config[Numero_definition_option].Suppr = Deletable; + Numero_definition_option ++ ; +} + +/* Initialize configuration */ +bool Initialiser_config() +{ + bool Erreur = false; + FILE* Fichier_INI; + + Numero_definition_option = 0; + + Definir_option(0,"Scroll up", + "Scrolls the picture upwards, both in magnify and normal mode.", + "This hotkey cannot be removed.", + false, 0x48); // HAUT + Definir_option(1,"Scroll down", + "Scrolls the picture upwards, both in magnify and normal mode.", + "This hotkey cannot be removed.", + false, 0x48); // HAUT + Definir_option(2,"Scroll left", + "Scrolls the picture upwards, both in magnify and normal mode.", + "This hotkey cannot be removed.", + false, 0x48); // HAUT + Definir_option(3,"Scroll right", + "Scrolls the picture upwards, both in magnify and normal mode.", + "This hotkey cannot be removed.", + false, 0x48); // HAUT +} + +uint8_t Fenetre_choix(uint8_t Largeur, uint8_t Hauteur, char* Titre, char* Choix, uint8_t Choix_debut, + uint8_t Couleur,uint8_t Couleur_choix) +{ + puts("FENETRE CHOIX UNIMPLEMENTED !!!"); + return 0; +} + +void Test_duplic() +{ + puts("TEST DUPLIC UNIMPLEMENTED"); +} + +/* Checks if everything is OK */ +bool Validation() +{ + bool Y_a_des_erreurs = false; + uint16_t i = 0; + + while(i