danzeff OSK

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

danzeff OSK

Post by danzel »

I'm making available the OSK I introduced with AFKIM2 under the BSD license so other projects can make use of it.
It was designed by myself and Jeff Chen from the pspupdates forums.

Latest Version: 2 -- http://localhost.geek.nz/crap/danzeff_src_2.zip

Currently there are only implementations for SDL and the PSP GU, please check the included info and add an implementation for something new :) and I can add it to the official distribution.

Any questions, just ask.
Last edited by danzel on Wed Apr 26, 2006 2:34 pm, edited 1 time in total.
User avatar
zx-81
Posts: 11
Joined: Fri Apr 07, 2006 5:38 pm
Location: France
Contact:

Post by zx-81 »

Hi Danzel and Jeff,

Thanks to have let your work under BSD license. I've just released a CPC emulator (caprice32 port) using your nice keyboard.

PSPCap32

Caprice32 is GNU, so PSPCap32 is also GNU and sources are included.
The only good is knowledge and the only evil is ignorance.
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

I had a look at that, nice work :)
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

Thanks to Jesper we now have an implementation for the PSP GU renderer. (No other changes)

Grab the new code at http://localhost.geek.nz/crap/danzeff_src_2.zip

If someone wants to add danzeff to the ps2dev svn then please go ahead, or get me a login and I will do it :)
User avatar
zx-81
Posts: 11
Joined: Fri Apr 07, 2006 5:38 pm
Location: France
Contact:

Post by zx-81 »

@Danzel : Please, what are the name of the font you have used for you keyboard ? I would like to add some more keys in the remaining free space (on digit squares). I'm not a graphic designer, so i can't reconize the font ...

Thanks.
The only good is knowledge and the only evil is ignorance.
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

bitstream vera sans mono I think it would be...
The raws are available at: http://localhost.geek.nz/crap/newkey3raw.zip
If you want to take a look, open them with gimp.
User avatar
zx-81
Posts: 11
Joined: Fri Apr 07, 2006 5:38 pm
Location: France
Contact:

Post by zx-81 »

Many thanks Danzel :D
The only good is knowledge and the only evil is ignorance.
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

id love to see this in native C :)
with a simplified sample with blank
black screen and then when user
press start OSK pop up and thats all
on screen
10011011 00101010 11010111 10001001 10111010
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

danzel: I want to use your OSK,but there are some problems.



psp-gcc -I. -Id:/pspdev/psp/sdk/include -O2 -G0 -Wall -c -o danzeff.o danzeff.c
psp-gcc -I. -Id:/pspdev/psp/sdk/include -O2 -G0 -Wall -L. -Ld:/pspdev/psp/sdk/lib main.o danzeff.o -lpng -lz -lm -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lc -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspuser -lpspkernel -o PSP_Mobile.elf
danzeff.o: In function `surface_draw_offset':
danzeff.c:(.text+0x2f4): undefined reference to `sceGuAlphaFunc'
danzeff.c:(.text+0x2fc): undefined reference to `sceGuEnable'
danzeff.c:(.text+0x308): undefined reference to `sceGuTexFunc'
danzeff.c:(.text+0x310): undefined reference to `sceGuTexEnvColor'
danzeff.c:(.text+0x328): undefined reference to `sceGuBlendFunc'
danzeff.c:(.text+0x330): undefined reference to `sceGuEnable'
danzeff.c:(.text+0x344): undefined reference to `sceGuTexMode'
danzeff.c:(.text+0x35c): undefined reference to `sceGuTexImage'
danzeff.c:(.text+0x368): undefined reference to `sceGuTexFunc'
danzeff.c:(.text+0x370): undefined reference to `sceGuGetMemory'
danzeff.c:(.text+0x42c): undefined reference to `sceGuDrawArray'
danzeff.c:(.text+0x434): undefined reference to `sceGuDisable'
danzeff.c:(.text+0x460): undefined reference to `sceGuDisable'
collect2: ld returned 1 exit status
make: *** [PSP_Mobile.elf] Error 1

found that in danzeff.c has included "pspgu.h" . this is why?
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

You need to link with -lpspgu
Shoot Pixels Not People!
Makeshift Development
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

successful,thank you very much
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

is there a non lua C version
laying around anywhere of danzeff OSK?
10011011 00101010 11010111 10001001 10111010
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

The one in the first post of this thread?
The current official (c) version with a few modifications lives in the pspradio svn, http://sourceforge.net/svn/?group_id=166538
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

danzel: When I used your OSK,I meet the problem,which is that when I run the danzeff_render() function,the psp power off. I found that the program stopped the sceGuAlphaFunc( GU_GREATER, 0, 0xff ) function.Can you tell me this why . I have the psp running on FW2.5
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Post by sandberg »

twuking wrote:danzel: When I used your OSK,I meet the problem,which is that when I run the danzeff_render() function,the psp power off. I found that the program stopped the sceGuAlphaFunc( GU_GREATER, 0, 0xff ) function.Can you tell me this why . I have the psp running on FW2.5
If you use the GU version of Danzeff, you has to make sure that you have an active displaylist. I've written a short manual in the danzeff_readme.txt file.
Br, Sandberg
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

Thank you for your reply.

Before I run the danzeff_render() function,I have run the danzeff_load()and danzeff_isinitialized() returns true.There are some anything else,what I didnt do.
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Post by sandberg »

twuking wrote:Thank you for your reply.

Before I run the danzeff_render() function,I have run the danzeff_load()and danzeff_isinitialized() returns true.There are some anything else,what I didnt do.
Yep, danzeff_load will load the PNG images from your memory-stick and if that goes okay then danzeff_isinitialized will return true.

But note, there are 3 versions of danzeff in the same file. One which uses SDL, one which uses the GU and one which writes directly to VRAM. Which version that fits your own code best, can be selected by defining it on the command-line. You can see the defines needed, in the sourcecode.
Br, Sandberg
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

thanx

3 version !? I have found 2. One is SDL ,the other si GU.I have defined the DANZEFF_SCEGU in the danzeff.h file.
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

//Set which renderer target to build for
/* #define DANZEFF_SDL */
/* #define DANZEFF_SCEGU */

have changed to

//Set which renderer target to build for
/* #define DANZEFF_SDL */
#define DANZEFF_SCEGU
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Post by sandberg »

This is not from the latest version. There are a third version now (direct writings to VRAM, which is used in the Links2 browser).

The definitions above are not used anymore.
Br, Sandberg
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

Code: Select all

svn co https://svn.sourceforge.net/svnroot/pspradio/trunk/PSP/SharedLib/danzeff
To check out the newest copy.

Compile the vram version with make -f Makefile.vram
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

I want to use GU. How to?
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

In GU , where need the displaylist. How to get it. Is the guiStrings the displaylist?
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

Can you support an example?
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

I have gotten the new version.but there are new problems

Makefile:2: ../../include.mak: No such file or directory
make: *** No rule to make target `../../include.mak'. Stop.
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

I dont understand that GU how to work. Can you give me files or url about GU. Thanx
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

This is my program

static unsigned int __attribute__((aligned(16))) list[262144];

danzeff_load();


while(!done)
{
if(danzeff_dirty())
{
sceGuStart(GU_DIRECT,list);
danzeff_render();
sceGuFinish();
}
.
.
.
}

need what else
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Post by sandberg »

Basically that should work. Assuming you have set up your screens etc. ?

I use the GU version in PSPTris my self. The sources are in the PSPRadio subversion repository also (skip the SharedLib/danzeff on checkout). Then under Plugins/GAME_PSPTris you can try to take a look at the code. Just grep for danzeff.
Br, Sandberg
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

now my program dont stop! I have fixed it .but still have worked wrongly.

Code: Select all


#include <pspkernel.h>
#include <pspdebug.h> 
#include <stdlib.h>
#include <string.h>
#include <pspgu.h>
#include "danzeff.h"



#define printf pspDebugScreenPrintf

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;

PSP_MODULE_INFO&#40;"PSP_Mobile", 0, 1, 1&#41;; 

int done = 0;
// Exit callback
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
      done = 1;
      sceKernelExitGame&#40;&#41;;
      return 0;
&#125;

// Callback thread
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
      int cbid;

      cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
      sceKernelRegisterExitCallback&#40;cbid&#41;;

      sceKernelSleepThreadCB&#40;&#41;;

      return 0;
&#125;

// Sets up the callback thread and returns its thread id
int SetupCallbacks&#40;void&#41;
&#123;
      int thid = 0;

      thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
      if&#40;thid >= 0&#41; 
      &#123;
            sceKernelStartThread&#40;thid, 0, 0&#41;;
      &#125;

      return thid;
&#125; 


int main&#40;void&#41;
&#123;
	SceCtrlData pad;
	char key;

	pspDebugScreenInit&#40;&#41;;
	SetupCallbacks&#40;&#41;;

	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;
	
	
	
               danzeff_load&#40;&#41;;
	if &#40;!&#40;danzeff_isinitialized&#40;&#41;&#41;&#41;
	&#123;
	return 0;
	&#125;
			
               danzeff_moveTo&#40;0,0&#41;; 
	
               while&#40;!done&#41;
               &#123;
	if&#40;danzeff_dirty&#40;&#41;&#41;
	&#123;
	   sceGuInit&#40;&#41;; 
	   sceGuStart&#40;GU_DIRECT,list&#41;;
	   danzeff_render&#40;&#41;;
	   sceGuFinish&#40;&#41;;				
	&#125;
                pspDebugScreenSetXY&#40;0, 8&#41;;
                sceCtrlReadBufferPositive&#40;&pad, 1&#41;; 
                key=danzeff_readInput&#40;pad&#41;;
                switch &#40;key&#41;
               &#123;
    	   case 0&#58;
    	   printf&#40;"You have pressed nothing!\n"&#41;;
    	   break;
    	  case 8&#58;
    	  case '\n'&#58;
    	  case DANZEFF_LEFT&#58;
    	  case DANZEFF_RIGHT&#58;
    	  case DANZEFF_SELECT&#58;
    	  case DANZEFF_START&#58;
    	     printf&#40;"You have put the unshowed key\n"&#41;;
    	     break;
    	  default&#58;
    	      printf&#40;"You have input the key %c\n",key&#41;;
    	      waitOneSecond&#40;&#41;;
    	      break;
    	&#125;
              printf&#40;"Analog X = %d ", pad.Lx&#41;;
              printf&#40;"Analog Y = %d \n", pad.Ly&#41;;
              pspDebugScreenSetXY&#40;0, 15&#41;;
              if &#40;pad.Buttons != 0&#41;&#123;
	if &#40;pad.Buttons & PSP_CTRL_SQUARE&#41;&#123;
	   printf&#40;"Square pressed \n"&#41;;
	&#125;
	if &#40;pad.Buttons & PSP_CTRL_TRIANGLE&#41;&#123;
	   printf&#40;"Triangle pressed \n"&#41;;
	&#125; 
	if &#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;&#123;
	   printf&#40;"Cicle pressed \n"&#41;;
	&#125; 
	if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;&#123;
	   printf&#40;"Cross pressed \n"&#41;;
	&#125; 
                if &#40;pad.Buttons & PSP_CTRL_UP&#41;&#123;
	   printf&#40;"Up pressed \n"&#41;;
	&#125; 
	if &#40;pad.Buttons & PSP_CTRL_DOWN&#41;&#123;
	   printf&#40;"Down pressed \n"&#41;;
	&#125; 
	if &#40;pad.Buttons & PSP_CTRL_LEFT&#41;&#123;
	   printf&#40;"Left pressed \n"&#41;;
	&#125; 
	if &#40;pad.Buttons & PSP_CTRL_RIGHT&#41;&#123;
	   printf&#40;"Right pressed \n"&#41;;
	&#125;      
               if &#40;pad.Buttons & PSP_CTRL_START&#41;&#123;
	   printf&#40;"Start pressed \n"&#41;;
	&#125;
	if &#40;pad.Buttons & PSP_CTRL_SELECT&#41;&#123;
	   printf&#40;"Select pressed \n"&#41;;
	&#125;
	if &#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41;&#123;
	printf&#40;"L-trigger pressed \n"&#41;;
	&#125;
	if &#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41;&#123;
	printf&#40;"R-trigger pressed \n"&#41;;
	&#125;      
              &#125;
         &#125;
         sceKernelExitGame&#40;&#41;;
         return 0;
&#125;
I use the danzeff OSK ,which has sdl and gu. I want to use GU,so I have defined DANZEFF_SCEGU. now letters have inputed correctly,but pics arent showed.
twuking
Posts: 24
Joined: Thu Jun 22, 2006 7:27 pm

Post by twuking »

danzeff.c

Code: Select all

#include "danzeff.h"

// headerfiles needed for the GU version
#ifdef DANZEFF_SCEGU
#include <malloc.h>
#include "pspkernel.h"
#include "pspgu.h"
#include "png.h"
#endif // #ifdef DANZEFF_SCEGU

#define debug
#define printf pspDebugScreenPrintf

#define false 0
#define true 1

// structures used for drawing the keyboard
#ifdef DANZEFF_SCEGU
	struct danzeff_vertex
	&#123;
		float u, v;
		unsigned int color;
		float x,y,z;
	&#125;;

	struct danzeff_gu_surface
	&#123;
		u32		surface_width;
		u32		surface_height;
		u32		texture_width;
		u32		texture_height;
		u32		*texture;
	&#125;;

#endif //#ifdef DANZEFF_SCEGU

/*bool*/ int holding = false;     //user is holding a button
/*bool*/ int dirty = true;        //keyboard needs redrawing
/*bool*/ int shifted = false;     //user is holding shift
int mode = 0;             //charset selected. &#40;0 - letters or 1 - numbers&#41;
/*bool*/ int initialized = false; //keyboard is initialized

//Position on the 3-3 grid the user has selected &#40;range 0-2&#41;
int selected_x = 1;
int selected_y = 1;

//Variable describing where each of the images is
#define guiStringsSize 12 /* size of guistrings array */
#define PICS_BASEDIR "./graphics/"
char *guiStrings&#91;&#93; =
&#123;
	PICS_BASEDIR "keys.png", PICS_BASEDIR "keys_t.png", PICS_BASEDIR "keys_s.png",
	PICS_BASEDIR "keys_c.png", PICS_BASEDIR "keys_c_t.png", PICS_BASEDIR "keys_s_c.png",
	PICS_BASEDIR "nums.png", PICS_BASEDIR "nums_t.png", PICS_BASEDIR "nums_s.png",
	PICS_BASEDIR "nums_c.png", PICS_BASEDIR "nums_c_t.png", PICS_BASEDIR "nums_s_c.png"
&#125;;

//amount of modes &#40;non shifted&#41;, each of these should have a corresponding shifted mode.
#define MODE_COUNT 2
//this is the layout of the keyboard
char modeChar&#91;MODE_COUNT*2&#93;&#91;3&#93;&#91;3&#93;&#91;5&#93; =
&#123;
	&#123;	//standard letters
		&#123; ",abc",  ".def","!ghi" &#125;,
		&#123; "-jkl","\010m n", "?opq" &#125;,
		&#123; "&#40;rst",  "&#58;uvw","&#41;xyz" &#125;
	&#125;,

	&#123;	//capital letters
		&#123; "^ABC",  "@DEF","*GHI" &#125;,
		&#123; "_JKL","\010M N", "\"OPQ" &#125;,
		&#123; "=RST",  ";UVW","/XYZ" &#125;
	&#125;,

	&#123;	//numbers
		&#123; "\0\0\0001","\0\0\0002","\0\0\0003" &#125;,
		&#123; "\0\0\0004",  "\010\0 5","\0\0\0006" &#125;,
		&#123; "\0\0\0007","\0\0\0008", "\0\00009" &#125;
	&#125;,

	&#123;	//special characters
		&#123; "'&#40;.&#41;",  "\"<'>","-&#91;_&#93;" &#125;,
		&#123; "!&#123;?&#125;","\010\0 \0", "+\\=/" &#125;,
		&#123; "&#58;@;#",  "~$`%","*^|&" &#125;
	&#125;
&#125;;

/*bool*/ int danzeff_isinitialized&#40;&#41;
&#123;
	return initialized;
&#125;

/*bool*/ int danzeff_dirty&#40;&#41;
&#123;
	return dirty;
&#125;

/** Attempts to read a character from the controller
* If no character is pressed then we return 0
* Other special values&#58; 1 = move left, 2 = move right, 3 = select, 4 = start
* Every other value should be a standard ascii value.
* An unsigned int is returned so in the future we can support unicode input
*/
unsigned int danzeff_readInput&#40;SceCtrlData pspctrl&#41;
&#123;
	//Work out where the analog stick is selecting
	int x = 1;
	int y = 1;
	if &#40;pspctrl.Lx < 85&#41;     x -= 1;
	else if &#40;pspctrl.Lx > 170&#41; x += 1;

	if &#40;pspctrl.Ly < 85&#41;     y -= 1;
	else if &#40;pspctrl.Ly > 170&#41; y += 1;

	if &#40;selected_x != x || selected_y != y&#41; //If they've moved, update dirty
	&#123;
		dirty = true;
		selected_x = x;
		selected_y = y;
	&#125;
	//if they are changing shift then that makes it dirty too
	if &#40;&#40;!shifted && &#40;pspctrl.Buttons & PSP_CTRL_RTRIGGER&#41;&#41; || &#40;shifted && !&#40;pspctrl.Buttons & PSP_CTRL_RTRIGGER&#41;&#41;&#41;
		dirty = true;

	unsigned int pressed = 0; //character they have entered, 0 as that means 'nothing'
	shifted = &#40;pspctrl.Buttons & PSP_CTRL_RTRIGGER&#41;?true&#58;false;

	if &#40;!holding&#41;
	&#123;
		if &#40;pspctrl.Buttons& &#40;PSP_CTRL_CROSS|PSP_CTRL_CIRCLE|PSP_CTRL_TRIANGLE|PSP_CTRL_SQUARE&#41;&#41; //pressing a char select button
		&#123;
			int innerChoice = 0;
			if      &#40;pspctrl.Buttons & PSP_CTRL_TRIANGLE&#41;
				innerChoice = 0;
			else if &#40;pspctrl.Buttons & PSP_CTRL_SQUARE&#41;
				innerChoice = 1;
			else if &#40;pspctrl.Buttons & PSP_CTRL_CROSS&#41;
				innerChoice = 2;
			else //if &#40;pspctrl.Buttons & PSP_CTRL_CIRCLE&#41;
				innerChoice = 3;

			//Now grab the value out of the array
			pressed = modeChar&#91; mode*2 + shifted&#93;&#91;y&#93;&#91;x&#93;&#91;innerChoice&#93;;
		&#125;
		else if &#40;pspctrl.Buttons& PSP_CTRL_LTRIGGER&#41; //toggle mode
		&#123;
			dirty = true;
			mode++;
			mode %= MODE_COUNT;
		&#125;
		else if &#40;pspctrl.Buttons& PSP_CTRL_DOWN&#41;
		&#123;
			pressed = '\n';
		&#125;
		else if &#40;pspctrl.Buttons& PSP_CTRL_UP&#41;
		&#123;
			pressed = 8; //backspace
		&#125;
		else if &#40;pspctrl.Buttons& PSP_CTRL_LEFT&#41;
		&#123;
			pressed = DANZEFF_LEFT; //LEFT
		&#125;
		else if &#40;pspctrl.Buttons& PSP_CTRL_RIGHT&#41;
		&#123;
			pressed = DANZEFF_RIGHT; //RIGHT
		&#125;
		else if &#40;pspctrl.Buttons& PSP_CTRL_SELECT&#41;
		&#123;
			pressed = DANZEFF_SELECT; //SELECT
		&#125;
		else if &#40;pspctrl.Buttons& PSP_CTRL_START&#41;
		&#123;
			pressed = DANZEFF_START; //START
		&#125;
	&#125;

	holding = pspctrl.Buttons & ~PSP_CTRL_RTRIGGER; //RTRIGGER doesn't set holding

	return pressed;
&#125;

///-------------------------------------------------------------------------------
///These are specific to the implementation, they should have the same behaviour across implementations.
///-------------------------------------------------------------------------------


///-------------------------------------------------------------------------------
///This is the original SDL implementation
#ifdef DANZEFF_SDL

SDL_Surface* keyBits&#91;guiStringsSize&#93;;
int keyBitsSize = 0;
int moved_x = 0, moved_y = 0; // location that we are moved to

///variable needed for rendering in SDL, the screen surface to draw to, and a function to set it!
SDL_Surface* danzeff_screen;
SDL_Rect danzeff_screen_rect;
void danzef_set_screen&#40;SDL_Surface* screen&#41;
&#123;
	danzeff_screen = screen;
	danzeff_screen_rect.x = 0;
	danzeff_screen_rect.y = 0;
	danzeff_screen_rect.h = screen->h;
	danzeff_screen_rect.w = screen->w;
&#125;


///Internal function to draw a surface internally offset
//Render the given surface at the current screen position offset by screenX, screenY
//the surface will be internally offset by offsetX,offsetY. And the size of it to be drawn will be intWidth,intHeight
void surface_draw_offset&#40;SDL_Surface* pixels, int screenX, int screenY, int offsetX, int offsetY, int intWidth, int intHeight&#41;
&#123;
	//move the draw position
	danzeff_screen_rect.x = moved_x + screenX;
	danzeff_screen_rect.y = moved_y + screenY;

	//Set up the rectangle
	SDL_Rect pixels_rect;
	pixels_rect.x = offsetX;
	pixels_rect.y = offsetY;
	pixels_rect.w = intWidth;
	pixels_rect.h = intHeight;

	SDL_BlitSurface&#40;pixels, &pixels_rect, danzeff_screen, &danzeff_screen_rect&#41;;
&#125;

///Draw a surface at the current moved_x, moved_y
void surface_draw&#40;SDL_Surface* pixels&#41;
&#123;
	surface_draw_offset&#40;pixels, 0, 0, 0, 0, pixels->w, pixels->h&#41;;
&#125;

/* load all the guibits that make up the OSK */
void danzeff_load&#40;&#41;
&#123;
	if &#40;initialized&#41; return;

	int a;
	for &#40;a = 0; a < guiStringsSize; a++&#41;
	&#123;
		keyBits&#91;a&#93; = IMG_Load&#40;guiStrings&#91;a&#93;&#41;;
		if &#40;keyBits&#91;a&#93; == NULL&#41;
		&#123;
			//ERROR! out of memory.
			//free all previously created surfaces and set initialized to false
			int b;
			for &#40;b = 0; b < a; b++&#41;
			&#123;
				SDL_FreeSurface&#40;keyBits&#91;b&#93;&#41;;
				keyBits&#91;b&#93; = NULL;
			&#125;
			initialized = false;
			return;
		&#125;
	&#125;
	initialized = true;
&#125;

/* remove all the guibits from memory */
void danzeff_free&#40;&#41;
&#123;
	if &#40;!initialized&#41; return;

	int a;
	for &#40;a = 0; a < guiStringsSize; a++&#41;
	&#123;
		SDL_FreeSurface&#40;keyBits&#91;a&#93;&#41;;
		keyBits&#91;a&#93; = NULL;
	&#125;
	initialized = false;
&#125;

/* draw the keyboard at the current position */
void danzeff_render&#40;&#41;
&#123;
	printf&#40;"Drawing Keyboard at %i,%i\n", selected_x, selected_y&#41;;
	dirty = false;

	///Draw the background for the selected keyboard either transparent or opaque
	///this is the whole background image, not including the special highlighted area
	//if center is selected then draw the whole thing opaque
	if &#40;selected_x == 1 && selected_y == 1&#41;
		surface_draw&#40;keyBits&#91;6*mode + shifted*3&#93;&#41;;
	else
		surface_draw&#40;keyBits&#91;6*mode + shifted*3 + 1&#93;&#41;;

	///Draw the current Highlighted Selector &#40;orange bit&#41;
	surface_draw_offset&#40;keyBits&#91;6*mode + shifted*3 + 2&#93;,
	//Offset from the current draw position to render at
	selected_x*43, selected_y*43,
	//internal offset of the image
	selected_x*64,selected_y*64,
	//size to render &#40;always the same&#41;
	64, 64&#41;;
&#125;

/* move the position the keyboard is currently drawn at */
void danzeff_moveTo&#40;const int newX, const int newY&#41;
&#123;
	moved_x = newX;
	moved_y = newY;
&#125;

#endif //DANZEFF_SDL


///-------------------------------------------------------------------------------
///This is the GU implementation
#ifdef DANZEFF_SCEGU

struct danzeff_gu_surface	keyTextures&#91;guiStringsSize&#93;;

int moved_x = 0, moved_y = 0; // location that we are moved to


///Internal function to draw a surface internally offset
//Render the given surface at the current screen position offset by screenX, screenY
//the surface will be internally offset by offsetX,offsetY. And the size of it to be drawn will be intWidth,intHeight
void surface_draw_offset&#40;struct danzeff_gu_surface* surface, int screenX, int screenY, int offsetX, int offsetY, int intWidth, int intHeight&#41;
&#123;
	sceGuAlphaFunc&#40; GU_GREATER, 0, 0xff &#41;;
	sceGuEnable&#40; GU_ALPHA_TEST &#41;;
	sceGuTexFunc&#40;GU_TFX_BLEND,GU_TCC_RGBA&#41;;
	sceGuTexEnvColor&#40;0xFF000000&#41;;
	sceGuBlendFunc&#40; GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0 &#41;;
	sceGuEnable&#40; GU_BLEND &#41;;
	sceGuTexMode&#40;GU_PSM_8888,0,0,GU_FALSE&#41;;
	sceGuTexImage&#40;0,surface->surface_width, surface->surface_height,surface->surface_width, surface->texture&#41;;
	sceGuTexFunc&#40;GU_TFX_MODULATE,GU_TCC_RGBA&#41;;

	struct danzeff_vertex* c_vertices = &#40;struct danzeff_vertex*&#41;sceGuGetMemory&#40;2 * sizeof&#40;struct danzeff_vertex&#41;&#41;;

	c_vertices&#91;0&#93;.u 		= offsetX;
	c_vertices&#91;0&#93;.v 		= offsetY;
	c_vertices&#91;0&#93;.x 		= moved_x + screenX;
	c_vertices&#91;0&#93;.y 		= moved_y + screenY;
	c_vertices&#91;0&#93;.z 		= 0;
	c_vertices&#91;0&#93;.color 	= 0xFFFFFFFF;

	c_vertices&#91;1&#93;.u 		= offsetX + intWidth;
	c_vertices&#91;1&#93;.v 		= offsetY + intHeight;
	c_vertices&#91;1&#93;.x 		= moved_x + screenX + intWidth;
	c_vertices&#91;1&#93;.y 		= moved_y + screenY + intHeight;
	c_vertices&#91;1&#93;.z 		= 0;
	c_vertices&#91;1&#93;.color 	= 0xFFFFFFFF;

	sceGuDrawArray&#40;GU_SPRITES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_2D,2,0,c_vertices&#41;;

	sceGuDisable&#40; GU_BLEND &#41;;
	sceGuDisable&#40; GU_ALPHA_TEST &#41;;
&#125;

void danzeff_block_copy&#40;struct danzeff_gu_surface* surface, u32 *texture&#41;
&#123;
	u32 *dest = surface->texture;
	u32 stride = surface->surface_width - surface->texture_width;
	u32 y, x;

	for &#40;y = 0 ; y < surface->texture_height ; y++&#41;
	&#123;
		for &#40;x = 0 ; x < surface->texture_width ; x++&#41;
		&#123;
		*dest++ = *texture++;
		&#125;
		// skip at the end of each line
		if &#40;stride > 0&#41;
		&#123;
			dest += stride;
		&#125;
	&#125;
&#125;

static void danzeff_user_warning_fn&#40;png_structp png_ptr, png_const_charp warning_msg&#41;
&#123;
	// ignore PNG warnings
&#125;

/* Get the width and height of a png image */
int danzeff_get_png_image_size&#40;const char* filename, u32 *png_width, u32 *png_height&#41;
&#123;
	png_structp png_ptr;
	png_infop info_ptr;
	unsigned int sig_read = 0;
	png_uint_32 width, height;
	int bit_depth, color_type, interlace_type;
	FILE *fp;

	if &#40;&#40;fp = fopen&#40;filename, "rb"&#41;&#41; == NULL&#41; return -1;
	png_ptr = png_create_read_struct&#40;PNG_LIBPNG_VER_STRING, NULL, NULL, NULL&#41;;
	if &#40;png_ptr == NULL&#41; &#123;
		fclose&#40;fp&#41;;
		return -1;
	&#125;
	png_set_error_fn&#40;png_ptr, &#40;png_voidp&#41; NULL, &#40;png_error_ptr&#41; NULL, danzeff_user_warning_fn&#41;;
	info_ptr = png_create_info_struct&#40;png_ptr&#41;;
	if &#40;info_ptr == NULL&#41; &#123;
		fclose&#40;fp&#41;;
		png_destroy_read_struct&#40;&png_ptr, png_infopp_NULL, png_infopp_NULL&#41;;
		return -1;
	&#125;
	png_init_io&#40;png_ptr, fp&#41;;
	png_set_sig_bytes&#40;png_ptr, sig_read&#41;;
	png_read_info&#40;png_ptr, info_ptr&#41;;
	png_get_IHDR&#40;png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL&#41;;

	png_destroy_read_struct&#40;&png_ptr, &info_ptr, png_infopp_NULL&#41;;
	fclose&#40;fp&#41;;

	*png_width = width;
	*png_height = height;
	return 0;
&#125;

/* Load a texture from a png image */
int danzeff_load_png_image&#40;const char* filename, u32 *ImageBuffer&#41;
&#123;
	png_structp png_ptr;
	png_infop info_ptr;
	unsigned int sig_read = 0;
	png_uint_32 width, height;
	int bit_depth, color_type, interlace_type;
	size_t x, y;
	u32* line;
	FILE *fp;

	if &#40;&#40;fp = fopen&#40;filename, "rb"&#41;&#41; == NULL&#41; return -1;
	png_ptr = png_create_read_struct&#40;PNG_LIBPNG_VER_STRING, NULL, NULL, NULL&#41;;
	if &#40;png_ptr == NULL&#41; &#123;
		fclose&#40;fp&#41;;
		return -1;
	&#125;
	png_set_error_fn&#40;png_ptr, &#40;png_voidp&#41; NULL, &#40;png_error_ptr&#41; NULL, danzeff_user_warning_fn&#41;;
	info_ptr = png_create_info_struct&#40;png_ptr&#41;;
	if &#40;info_ptr == NULL&#41; &#123;
		fclose&#40;fp&#41;;
		png_destroy_read_struct&#40;&png_ptr, png_infopp_NULL, png_infopp_NULL&#41;;
		return -1;
	&#125;
	png_init_io&#40;png_ptr, fp&#41;;
	png_set_sig_bytes&#40;png_ptr, sig_read&#41;;
	png_read_info&#40;png_ptr, info_ptr&#41;;
	png_get_IHDR&#40;png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL&#41;;
	png_set_strip_16&#40;png_ptr&#41;;
	png_set_packing&#40;png_ptr&#41;;
	if &#40;color_type == PNG_COLOR_TYPE_PALETTE&#41; png_set_palette_to_rgb&#40;png_ptr&#41;;
	if &#40;color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8&#41; png_set_gray_1_2_4_to_8&#40;png_ptr&#41;;
	if &#40;png_get_valid&#40;png_ptr, info_ptr, PNG_INFO_tRNS&#41;&#41; png_set_tRNS_to_alpha&#40;png_ptr&#41;;
	png_set_filler&#40;png_ptr, 0xff, PNG_FILLER_AFTER&#41;;
	line = &#40;u32*&#41; malloc&#40;width * 4&#41;;
	if &#40;!line&#41; &#123;
		fclose&#40;fp&#41;;
		png_destroy_read_struct&#40;&png_ptr, png_infopp_NULL, png_infopp_NULL&#41;;
		return -1;
	&#125;
	for &#40;y = 0; y < height; y++&#41;
	&#123;
		png_read_row&#40;png_ptr, &#40;u8*&#41; line, png_bytep_NULL&#41;;
		for &#40;x = 0; x < width; x++&#41;
		&#123;
			ImageBuffer&#91;y*width+x&#93; = line&#91;x&#93;;
		&#125;
	&#125;
	free&#40;line&#41;;
	png_read_end&#40;png_ptr, info_ptr&#41;;
	png_destroy_read_struct&#40;&png_ptr, &info_ptr, png_infopp_NULL&#41;;
	fclose&#40;fp&#41;;
	return 0;
&#125;

u32 danzeff_convert_pow2&#40;u32 size&#41;
&#123;
	u32 pow_counter = 0;

	for &#40; ; pow_counter < 32 ; pow_counter++&#41;
	&#123;
		// Find the first value which is higher
		if &#40;&#40;size >> pow_counter&#41; == 0&#41;
		&#123;
			// take already good values into account
			if &#40;&#40;1 << pow_counter&#41; != size&#41;
			&#123;
				return &#40;1 << pow_counter&#41;;
			&#125;
			else
			&#123;
				return &#40;1 << &#40;pow_counter-1&#41;&#41;;
			&#125;
		&#125;
	&#125;
	return 0;
&#125;

/* load all the guibits that make up the OSK */
void danzeff_load&#40;&#41;
&#123;
	u32 *temp_texture;

	if &#40;initialized&#41; return;

	int a;
	for &#40;a = 0; a < guiStringsSize; a++&#41;
	&#123;
		u32 height, width;
	
		if &#40;danzeff_get_png_image_size&#40;guiStrings&#91;a&#93;, &width, &height&#41; == 0&#41;
		&#123;
			// The png is always converted to PSM_8888 format when read
			temp_texture = &#40;u32 *&#41;malloc&#40;width*height*4&#41;;
			if &#40;danzeff_load_png_image&#40;guiStrings&#91;a&#93;, temp_texture&#41; != 0&#41;
			&#123;
				// Error .. Couldn't get png info from one of the needed files
				int b;
	
				for &#40;b = 0; b < a; b++&#41;
				&#123;
					free&#40;keyTextures&#91;b&#93;.texture&#41;;
					keyTextures&#91;b&#93;.texture = NULL;
				&#125;
				initialized = false;
				return;
			&#125;
			else
			&#123;
				// we need to store the texture in an image of width and heights of 2^n sizes
				keyTextures&#91;a&#93;.texture_width 	= &#40;float&#41;width;
				keyTextures&#91;a&#93;.texture_height 	= &#40;float&#41;height;
				keyTextures&#91;a&#93;.surface_width 	= &#40;float&#41;danzeff_convert_pow2&#40;width&#41;;
				keyTextures&#91;a&#93;.surface_height 	= &#40;float&#41;danzeff_convert_pow2&#40;height&#41;;
				keyTextures&#91;a&#93;.texture 			= &#40;u32 *&#41;malloc&#40;keyTextures&#91;a&#93;.surface_width*keyTextures&#91;a&#93;.surface_height*4&#41;;
				// block copy the texture into the surface
				danzeff_block_copy&#40;&keyTextures&#91;a&#93;, temp_texture&#41;;
				free&#40;temp_texture&#41;;
			&#125;
		&#125;
		else
		&#123;
			// Error .. Couldn't get png info from one of the needed files
			int b;
	
			for &#40;b = 0; b < a; b++&#41;
			&#123;
				free&#40;keyTextures&#91;b&#93;.texture&#41;;
				keyTextures&#91;b&#93;.texture = NULL;
			&#125;
			initialized = false;
			return;
		&#125;
	&#125;
	initialized = true;
&#125;

/* remove all the guibits from memory */
void danzeff_free&#40;&#41;
&#123;
	if &#40;!initialized&#41; return;

	int a;
	for &#40;a = 0; a < guiStringsSize; a++&#41;
	&#123;
		free&#40;keyTextures&#91;a&#93;.texture&#41;;
		keyTextures&#91;a&#93;.texture = NULL;
	&#125;
	initialized = false;
&#125;

/* draw the keyboard at the current position */
void danzeff_render&#40;&#41;
&#123;
	dirty = false;

	///Draw the background for the selected keyboard either transparent or opaque
	///this is the whole background image, not including the special highlighted area
	//if center is selected then draw the whole thing opaque
	if &#40;selected_x == 1 && selected_y == 1&#41;
		surface_draw_offset&#40;&keyTextures&#91;6*mode + shifted*3&#93;, 0, 0, 0, 0, keyTextures&#91;6*mode + shifted*3&#93;.texture_width,
																		  keyTextures&#91;6*mode + shifted*3&#93;.texture_height&#41;;
	else
		surface_draw_offset&#40;&keyTextures&#91;6*mode + shifted*3 + 1&#93;, 0, 0, 0, 0, keyTextures&#91;6*mode + shifted*3 + 1&#93;.texture_width,
																			  keyTextures&#91;6*mode + shifted*3 + 1&#93;.texture_height&#41;;

  #ifdef debug
		printf&#40;" First surface_draw_offset&#40;&#41; has ran \n"&#41;;
		#endif
	///Draw the current Highlighted Selector &#40;orange bit&#41;
	surface_draw_offset&#40;&keyTextures&#91;6*mode + shifted*3 + 2&#93;,
	//Offset from the current draw position to render at
	selected_x*43, selected_y*43,
	//internal offset of the image
	selected_x*64,selected_y*64,
	//size to render &#40;always the same&#41;
	64, 64&#41;;
	#ifdef debug
		printf&#40;" Second  surface_draw_offset&#40;&#41; has ran \n"&#41;;
		#endif
&#125;

/* move the position the keyboard is currently drawn at */
void danzeff_moveTo&#40;const int newX, const int newY&#41;
&#123;
	moved_x = newX;
	moved_y = newY;
&#125;

#endif //DANZEFF_SCEGU
Post Reply