[fpc-devel] sdlgraph, pre-alpha
Evgeniy Ivanov
lolkaantimat at gmail.com
Wed Aug 29 12:07:51 CEST 2007
2007/8/29, Tomas Hajny <XHajT03 at mbox.vol.cz>:
>
> I think that I know the way to
> > not
> > implement sdlcrt and use the current crt unit.
> > But it needs thread and pipe, I never used them, but it won't be
> > complicated.
>
> I guess that it might be useful to discuss your idea regarding cooperation
> with Crt unit little bit more here (at least I'm interested what your
> plans are).
The problem with input is caused by SDL thread. And the same problem we may
find in C programmes, see C example (I took it from SDL tutorial, it's huge
because they decide to show some sdlutils things):
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
void Slock(SDL_Surface *screen);
void Sulock(SDL_Surface *screen);
void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8
B);
void DrawScene(SDL_Surface *screen);
/* ------------------------------------------- */
int main(int argc, char *argv[]){
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ){
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_WM_SetCaption("SDL Gfx Example #1","ex1");
SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"), NULL);
SDL_Surface *screen;
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL ){
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
int done=0;
/*while(done == 0){
SDL_Event event;
while ( SDL_PollEvent(&event) ){
if ( event.type == SDL_QUIT ){
done = 1;
}
if ( event.type == SDL_KEYDOWN ){
if ( event.key.keysym.sym == SDLK_ESCAPE ){
done = 1;
}
}
}
DrawScene(screen);
} */
// New code to show input problems
DrawScene(screen);
printf("Enter any number to finish the programme");
scanf("%d",&done);
}
/* ------------------------------------------------------ */
void Slock(SDL_Surface *screen){
if ( SDL_MUSTLOCK(screen) ){
if ( SDL_LockSurface(screen) < 0 ){
return;
}
}
}
/* ------------------------------------------------------ */
void Sulock(SDL_Surface *screen){
if ( SDL_MUSTLOCK(screen) ){
SDL_UnlockSurface(screen);
}
}
/* ------------------------------------------------------ */
void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8
B){
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel){
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *bufp = color;
} break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color;
} break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN){
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
}else{
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
} break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
} break;
}
}
/* ------------------------------------------------------ */
void DrawScene(SDL_Surface *screen){
Slock(screen);
for(int x=0;x<640;x++){
for(int y=0;y<480;y++){
DrawPixel(screen, x,y,y/2,y/2,x/3);
}
}
Sulock(screen);
SDL_Flip(screen);
}
/* ------------------------------------------- */
The same as in the sdlgraph (or fpc programmes with JEDI-SDL and readln/crt
routines).
SDL creates it's own thread. I don't know how (maybe forks), but it's
details.
So, I decide that we need to catch input in the separate thread with SDL
routines, like
while(done == 0){
SDL_Event event;
while ( SDL_PollEvent(&event) ){
if ( event.type == SDL_QUIT ){
done = 1;
}
if ( event.type == SDL_KEYDOWN ){
if ( event.key.keysym.sym == SDLK_ESCAPE ){
done = 1;
}
}
}
And then put (pipe) it to the standart input (to the fpc input). We may
create this thread in the initMode routine and kill in the closeGraph.
All code may be written in Pascal, it's just an example.
My friends helped me with this idea and also advised to use
int mkfifo(const char * pathname, mode_t mode);
or its pascal prototype.
--
E.I.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20070829/268532a0/attachment.html>
More information about the fpc-devel
mailing list