Dark Bit Factory & Gravity

PROGRAMMING => Purebasic => Topic started by: maracuja on August 01, 2011

Title: Requiring helps for purebasic.
Post by: maracuja on August 01, 2011
Code: [Select]
Global s$ ; make s$ global
; Constants

#WINTITLE  = "E-Swat - Replicants Remake"
#WINHEIGHT = 480 ; minimal rez 640x480
#WINWIDTH  = 640
#BPP          = 32

;Procs declaration
Declare getLetterPos(charac$)
Declare getFonts(IdSprite, x, y, w, h, num, r, g, b)
Declare ErrorWindowCreation()
Declare Anim(fullscreen.b)
Declare PrintTextWindows(text$)

;Include text
;XIncludeFile "Payload.pbi"


s$ = « BALABLAHAHAHAHHAHABBBBBBBBBBBBBBBB » + chr(10) + chr(13)
s$ = s$ + « NNNNNNNNNNNNNNNNNNNNNNNNNNNIIIIIIIIIIIIIIIIIIIIIHHHHHHHHHHHH »


Procedure getLetterPos(charac$)

  alpha$ =          " !" + Chr(34) + "    '()*+,-. 0123456789:; = ? ABCDEFG"
  alpha$ = alpha$ + "HIJKLMNOPQRSTUVWXYZ"
 

    length = Len(alpha$);

    i = 1;

    While (Mid(alpha$, i, 1) <> Mid(charac$, 1, 1) And i <= length)
      i = i + 1
    Wend

    If i > length
      ProcedureReturn 0
    EndIf
   
    ProcedureReturn i; 
EndProcedure


Procedure getFonts(IdSprite, x, y, w, h, num, r, g, b)
  fontsW = SpriteWidth(IdSprite) ; [!] Nota Ne ps enlever
  ; ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
  ; Reinit clipping with original fonts size for w and h
  posX = (num-1) % ((fontsW / w))
  posY = (num-1) / ((fontsW / w))
  srcX = (posX) * w
  srcY = (posY) * h
  srcH = h
  srcW = w
 
  ClipSprite(IdSprite, srcX, srcY, srcW, srcH)
  TransparentSpriteColor(IdSprite, RGB(r,g,b))
  DisplayTransparentSprite(IdSprite, x, y)
  ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
EndProcedure

Procedure ErrorWindowCreation()
  MessageRequester("Error", "This is fucked up, I can't create the window", 0)
  End
EndProcedure

Procedure Anim(fullscreen.b)
  InitSprite()
  InitKeyboard()
  If fullscreen
    If OpenScreen(#WINWIDTH, #WINHEIGHT, #BPP, #WINTITLE) = 0
      ErrorWindowCreation()
    EndIf
  Else
    If OpenWindow(1337,0,0,#WINWIDTH, #WINHEIGHT, #WINTITLE, #PB_Window_BorderLess | #PB_Window_ScreenCentered)
      If Not OpenWindowedScreen(WindowID(1337), 0, 0, #WINWIDTH, #WINHEIGHT, 1, 0, 0)
        ErrorWindowCreation()
      EndIf
    Else
       ErrorWindowCreation()
    EndIf
  EndIf
  ;Animation
  CatchSprite(0, ?fontsReps, #PB_Sprite_Texture)
  CreateSprite(12, #WINWIDTH, #WINHEIGHT)
  ;ClearScreen(0)

    xpad = 0
    ypad = 0
    StartDrawing(SpriteOutput(12))   ; Here i waiting to draw in id 12 but i saw in a screen...
    For i = 1 To Len(s$) Step 1
      car$ = Mid(s$, i , 1)
      Select car$
        Case Chr(10)
          ypad = ypad + 8
        Case Chr(13)
          xpad = 0
        Default
          getFonts(0,xpad, ypad+156, 8, 8, getLetterPos(car$), 0, 0, 0)
          xpad = xpad + 8
      EndSelect
    Next i
    StopDrawing()
   
   ; ClearScreen(0)
    StartDrawing(ScreenOutput())
   ; DisplaySprite(12,0,490) ; Here 490 in y but it printed screen
    StopDrawing()
    FlipBuffers()
    Repeat
    If fullscreen = 0
      Event = WaitWindowEvent(0)
    EndIf
    ExamineKeyboard()
  Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Space) Or KeyboardPushed(#PB_Key_T)
 
EndProcedure


Procedure PrintTextWindows(text$)
  Result = MessageRequester(#WINTITLE, « Lalala i m a message"+ Chr(10) + Chr(13) + "Do you want Fullscreen ? :)",#PB_MessageRequester_YesNoCancel)
  Select Result
    Case #PB_MessageRequester_Yes
      ;PrintTextConsole("fullscreen activated")
      Anim(1)
    Case #PB_MessageRequester_No
      ;PrintTextConsole("windowed activated")
      Anim(0)
    EndSelect
EndProcedure



;!!!!! Begin !!!!!
  PrintTextWindows(s$)

;!!!!! End !!!!!
DataSection
  fontsReps: IncludeBinary("eswatFonts.bmp ») ; here it’s a 8x8 bitmap font
EndDataSection


Thanks you for your help :)
Title: Re: Requiring helps for purebasic.
Post by: va!n on August 01, 2011
Cant test your source atm. Pls add next time a zip with all needed files, which may help to see the problem and solve it. Btw, i taked a fast look to your source and i am really wondering, why are you using following lines outside ot the mainloop?

Code: [Select]
;!!!!! Begin !!!!!
  PrintTextWindows(s$)

Makes no sence to me, becuase the program will be determinated. Add END if your program should exit
Title: Re: Requiring helps for purebasic.
Post by: padman on August 01, 2011
I don't really have time now to analyze the code (it's quite confusing to be honest  :xmas:), but va!n is right, would be cool if you could provide the used font so we can test the code. In general you can only draw Images on a Sprite with DX9 so the

Code: [Select]
  StartDrawing(SpriteOutput(12))
can't work at first sight. Will have a closer look at it tomorrow, if nobody else is faster. (Jace? Jaaaaace!!!  ;D)

Have a look at Shockwave's PB scroller example in the meantime, it's good and easy to understand (well, I'd say so at least ;)) Maybe you can adapt something from it.

http://www.dbfinteractive.com/forum/index.php?topic=1436.0 (http://www.dbfinteractive.com/forum/index.php?topic=1436.0)
Title: Re: Requiring helps for purebasic.
Post by: va!n on August 01, 2011
Clean up your source, because its really strange! PrintTextWIndows(a$) will ask if fullscreen or not, so name this procedure something like "SelectScreenmode()"! Btw, inside this procedure you are calling Anim(0) - another procedure where the Init stuff and OpenScreen stuff exist and all the drawing with the mainloop. Thats strange! Change your source to something like this:

Code: [Select]
;// All the init stuff at programstart

InitSprite()
Init...

Procedure AskFullscreen()
   ProcedureReturn ValueIfFullScreenOrNot
EndProcedure

Procedure OpenScreen()
   ProcedureReturn IfSuccessfullOrNot
EndProcedure

Procedure WhateverYouNeed()
   ProcedureReturn
EndProcedure

;// Start program

AskFullScreen()
OpenScreen()

;// Here the Mainloop

Repeat
   ClearScreen(0)
   ExamineKeyboard()

   AnyDrawingThings...

   FlipBuffers()
Until KEyboardPushed(...)

End    ; Before program may run into data section and crash when quit app

;// DataSection

Include....

[code]

Btw, you cant draw a sprite on another. Had the same problem when i wanted to code something for the last competition but failed due fact of PB limitaations: Check it here:
[url=http://www.purebasic.fr/english/viewtopic.php?f=16&t=46990]http://www.purebasic.fr/english/viewtopic.php?f=16&t=46990[/url]
Title: Re: Requiring helps for purebasic.
Post by: maracuja on August 02, 2011
Cant test your source atm. Pls add next time a zip with all needed files, which may help to see the problem and solve it. Btw, i taked a fast look to your source and i am really wondering, why are you using following lines outside ot the mainloop?

Code: [Select]
;!!!!! Begin !!!!!
  PrintTextWindows(s$)

Makes no sence to me, becuase the program will be determinated. Add END if your program should exit

Clean up your source, because its really strange! PrintTextWIndows(a$) will ask if fullscreen or not, so name this procedure something like "SelectScreenmode()"! Btw, inside this procedure you are calling Anim(0) - another procedure where the Init stuff and OpenScreen stuff exist and all the drawing with the mainloop. Thats strange! Change your source to something like this:

Code: [Select]
;// All the init stuff at programstart

InitSprite()
Init...

Procedure AskFullscreen()
   ProcedureReturn ValueIfFullScreenOrNot
EndProcedure

Procedure OpenScreen()
   ProcedureReturn IfSuccessfullOrNot
EndProcedure

Procedure WhateverYouNeed()
   ProcedureReturn
EndProcedure

;// Start program

AskFullScreen()
OpenScreen()

;// Here the Mainloop

Repeat
   ClearScreen(0)
   ExamineKeyboard()

   AnyDrawingThings...

   FlipBuffers()
Until KEyboardPushed(...)

End    ; Before program may run into data section and crash when quit app

;// DataSection

Include....

[code]

Btw, you cant draw a sprite on another. Had the same problem when i wanted to code something for the last competition but failed due fact of PB limitaations: Check it here:
[url=http://www.purebasic.fr/english/viewtopic.php?f=16&t=46990]http://www.purebasic.fr/english/viewtopic.php?f=16&t=46990[/url]

Thanks for watching my code. :)
In my initial code, i had two function PrintTextWindows and PrintTextConsole (but i removed the second to have cleaner code). PrintTextWindows, open the windows and make the work. PrintTextConsole is a unit test. It just used for validation of my loop, etc. Not important for the problem, imo. :)
For Anim, the semantics is : You wanna animate the program, let in windowed mode (0 or false) or fullscreen(1 or true), it's just a coding convention, never more never less. :)

I doesn't undertsand what is "strange" ? and for End statement, it's ok if you want but it's optional, i guess. :)




I don't really have time now to analyze the code (it's quite confusing to be honest  :xmas:), but va!n is right, would be cool if you could provide the used font so we can test the code. In general you can only draw Images on a Sprite with DX9 so the

Code: [Select]
  StartDrawing(SpriteOutput(12))
can't work at first sight. Will have a closer look at it tomorrow, if nobody else is faster. (Jace? Jaaaaace!!!  ;D)

Have a look at Shockwave's PB scroller example in the meantime, it's good and easy to understand (well, I'd say so at least ;)) Maybe you can adapt something from it.

http://www.dbfinteractive.com/forum/index.php?topic=1436.0 (http://www.dbfinteractive.com/forum/index.php?topic=1436.0)

I used CreateImage to build a 640x480x32 images and print on it my liner (fixed scrolltext). Of course, in this case i used StartDrawing(ImageOutput(12)) which are the handle of my pic and i used DrawImage to put the TransparentSprite on it. The result is the same like SpriteOutput... :/

Moreover, my code is not dirty ! imo

Edit: i commented the code, btw

PS : look the attachment for the code/gfx
Title: Re: Requiring helps for purebasic.
Post by: padman on August 02, 2011
Your code is fine and not dirty at all. The way you structure it is, let's put it, a little unusual. But hey, that's your style! I'll have a look at it now and see if I can fix it for you. Ttyl! ;)
Title: Re: Requiring helps for purebasic.
Post by: maracuja on August 02, 2011
Thanks you Padman, i'm happy to have the va!n opinion too but he hard bargain. :P
The debate, on the coding style are always interesting. :=)


Take care, I updated my attachment.
Title: Re: Requiring helps for purebasic.
Post by: padman on August 02, 2011
Ok mate I shoehorned in a scroller based on Shockwave's example. Hope it helps you a little. I don't have time for more at the moment. I think your approach is a little too complicated. Probably more Atari, but still. ;)

The changed Anim() proc looks like this now. Make sure to download the complete source though to see all changes (actually only some variables + the scroller string)

Code: [Select]
Procedure Anim(fullscreen.b)
  ;Init subsystem and the windows with fullscreen or not
  InitSprite()
  InitKeyboard()
  If fullscreen
    If OpenScreen(#WINWIDTH, #WINHEIGHT, #BPP, #WINTITLE) = 0
      ErrorWindowCreation()
    EndIf
  Else
    If OpenWindow(1337,0,0,#WINWIDTH, #WINHEIGHT, #WINTITLE, #PB_Window_BorderLess | #PB_Window_ScreenCentered)
      If Not OpenWindowedScreen(WindowID(1337), 0, 0, #WINWIDTH, #WINHEIGHT, 1, 0, 0)
        ErrorWindowCreation()
      EndIf
    Else
       ErrorWindowCreation()
    EndIf
  EndIf

  ;Animation
  CatchSprite(0, ?fontsReps, #PB_Sprite_Texture)
  CreateSprite(12, #WINWIDTH, #WINHEIGHT)

 ;####################################################################

   Repeat 
   
   
   ClearScreen(0)
   
    xpad = 0
    ypad = 0
 
    For i = 1 To Len(s$) Step 1
      car$ = Mid(s$, i , 1)
      Select car$
        Case Chr(10)
          ypad = ypad + 8
        Case Chr(13)
          xpad = 0
        Default
          getFonts(0,xpad, ypad+156, 8, 8, getLetterPos(car$), 0, 0, 0)
          xpad = xpad + 8
      EndSelect
    Next i
 
   
   
   

   

     cco = 0

  For cc = 0 To 25                                                    ; change to higher value to start further right
    letter   = Asc(Mid(s1$, tptr+cc, 1))-31                            ;your font is in ascii order, no need for special lookup index
 

   
 
    yCharPos = letter / 40
    xCharPos = letter % 40
   
      If xcharpos= 0: xcharpos = 40 : ycharpos=yCharPos-1  :EndIf
    If xCharPos <- 1: xCharPos = 39
 
    ElseIf xCharPos > 0  : xCharPos = xCharPos - 1
    EndIf
   
   
    ClipSprite(0, xCharPos*8, yCharPos*8, 8, 8)
    DisplaySprite(0, sco+cco, 320)
    cco = cco + 8
  Next
 

  sco = sco -2                                 ;change scrollspeed here
;     
  If sco < -8
    tptr = tptr + 1
    sco = sco + 8
  EndIf
;     
  If tptr > Len(s1$)-31
    tptr = 1
  EndIf
   
   

   ;##########################################################################
   
 
    FlipBuffers()

    If fullscreen = 0
      Event = WaitWindowEvent(0)
    EndIf
    ExamineKeyboard()
  Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Space) Or KeyboardPushed(#PB_Key_T)
 
EndProcedure
Title: Re: Requiring helps for purebasic.
Post by: maracuja on August 02, 2011
Ok,

So the "for loop" doesn't useful for your update, right ?

I can see the good fonts but the principle here is :
you loop on the letter a to z and you print it ?
The clipSprite are used just to pick the good letter in fonts bitmap buffer which is (handle = 0) ? and displaysprite pointed by handle = 0  fo rendering your letter on the final screen ?

To conclude, what's the interest to use screendrawing/stopdrawing if you can render without them ?
Do you think if it possible to create a pre-rendered buffer which have all fonts/logos and after i will play with this buffer ?
If you want, i will made a sample in c/sdl  to show the result ?
Title: Re: Requiring helps for purebasic.
Post by: padman on August 02, 2011
Quote
So the "for loop" doesn't useful for your update, right ?

Yours is still responsible for the static text. You can probably even use your version for displaying a scrolltext.

Quote
The clipSprite are used just to pick the good letter in fonts bitmap buffer which is (handle = 0) ? and displaysprite pointed by handle = 0  fo rendering your letter on the final screen ?

Yes, it clips a 8*8 rectangle of the larger font picture and displays that.

Quote
To conclude, what's the interest to use screendrawing/stopdrawing if you can render without them ?

StartDrawing()/StopDrawing() is only necessary for drawing Images or for PB internal drawing commands. For Sprites it's not needed, yes. Maybe you should have a look at the PB Help File and the Examples to understand the differences between Sprites and Images.

Quote
Do you think if it possible to create a pre-rendered buffer which have all fonts/logos and after i will play with this buffer ?
If you want, i will made a sample in c/sdl  to show the result ?

To be honest with you I don't exactly understand why you would want a pre-rendered buffer and what exactly you mean by that? I mean it's easy the way it's done now and it's not like you want to do a ton of gfx effects, where direct buffer drawing comes to mind. An example might help. I have to warn you though, I haven't got much experience with C, leave alone SDL.
Title: Re: Requiring helps for purebasic.
Post by: maracuja on August 02, 2011
Quote
So the "for loop" doesn't useful for your update, right ?

Yours is still responsible for the static text. You can probably even use your version for displaying a scrolltext.

Quote
The clipSprite are used just to pick the good letter in fonts bitmap buffer which is (handle = 0) ? and displaysprite pointed by handle = 0  fo rendering your letter on the final screen ?

Yes, it clips a 8*8 rectangle of the larger font picture and displays that.

Quote
To conclude, what's the interest to use screendrawing/stopdrawing if you can render without them ?

StartDrawing()/StopDrawing() is only necessary for drawing Images or for PB internal drawing commands. For Sprites it's not needed, yes. Maybe you should have a look at the PB Help File and the Examples to understand the differences between Sprites and Images.

Quote
Do you think if it possible to create a pre-rendered buffer which have all fonts/logos and after i will play with this buffer ?
If you want, i will made a sample in c/sdl  to show the result ?

To be honest with you I don't exactly understand why you would want a pre-rendered buffer and what exactly you mean by that? I mean it's easy the way it's done now and it's not like you want to do a ton of gfx effects, where direct buffer drawing comes to mind. An example might help. I have to warn you though, I haven't got much experience with C, leave alone SDL.

Ok for all :-) Thanks for you patience. :)

For pre-rendering look my c/sdl code in attachment, the code snippet :
Code: [Select]

#include <stdio.h>
#include <stdlib.h>
#if _MSC_VER || _WIN32
#include <windows.h>
#endif
#if _MSC_VER || _WIN32 || __APPLE__
#include <eswatFonts.h>
#include <eswatLogo.h>
#else
#include "eswatFonts.h"
#include "eswatLogo.h"
#endif
#include <assert.h>

#if !_MSC_VER
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

#define WIN_HEIGHT 640
#define WIN_WIDTH  480
#define FONT_HEIGHT 8
#define FONT_WIDTH 8
#define WIN_BPP      24
#define INTRONAME "E-Swat Replicants cracktro"

#if !defined(FALSE)
#define FALSE 0
#endif

#if !defined(TRUE)
#define TRUE 1
#endif

static int fin_prg = FALSE;
static int fin_anim = FALSE;
static int fin_scene1 = FALSE;

static void getFonts(SDL_Surface *fonts, SDL_Surface *screen, int x, int y, int w, int h, int num)
{
    int posX;
    int posY;
    SDL_Rect src;
    SDL_Rect dst;

    posX = (num-1) % ((fonts->w / w));
    posY = (num-1) / ((fonts->w / w));
    src.x = (posX) * w;
    src.y = (posY) * h;
    src.h = h;
    src.w = w;

    dst.x = x;
    dst.y = y;
    dst.h = src.h;
    dst.w = src.w;

    SDL_BlitSurface(fonts, &src, screen, &dst);
}

static int getLetterPos(char c)
{
    size_t i;
    size_t length;

    char alpha[] = {
        " !\"    '()*+,-. 0123456789:; = ? ABCDEFG" \
        "HIJKLMNOPQRSTUVWXYZ"
    };

    length = strlen(alpha);

    i = (size_t)0;

    while (alpha[i++] != c && i < length);

    if (i > length)
        return 0;

    return /*((int)c) - ((int) 'a' - (int)'A')*/ (int)(i);
  }

static void printText(SDL_Surface *screen, SDL_Surface *fonts, const char *s, int x, int y)
{
    size_t sLength;
    size_t i;

    sLength = strlen(s);

    for (i = 0; i < sLength ; i++)
    {
        getFonts(fonts, screen, (x+((int)i*8)), y, 8, 8, getLetterPos(s[i]));
    }
}

static void PrintPayload(SDL_Surface *screen, SDL_Surface *fonts)
{
    //liner
    size_t i;
    size_t length;
    int ypad;
    int xpad;
/*
 * 8x8
 * DC.B      '            ----------------            '
 * DC.B      '            -THE REPLICANTS-            '
 * DC.B      '            ----------------            ',$00
 * DC.B      '           PRESENTS: - ESWAT + -        '
 * DC.B      '                                        '
 * DC.B      'GREETZ TO : AVB,NEXT,AUTOMATION,TMB     '
 * DC.B      'THE POMPEY PIRATES,PHALANX,ALLIANCE,BB, '
 * DC.B      'DELIGHT,FOF,TLB,TDI,QUARTET,HTL,MCA,    '
 * DC.B      'THOR,MAD VISION,INNER CIRCLE,TSG...     ',$00
 * DC.B      '                                        '
 * DC.B      'MEGA THANX TO THE BWK FOR DIS LOGO...   ',$00
 * DC.B      '          BASH ',$27,'T',$27,' FOR TRAINER          ',$FF
 */
    char scrollText[] = {
        "            ----------------            \n\r" \
        "            -THE REPLICANTS-            \n\r" \
        "            ----------------            \n\r\n\r" \
        "           PRESENTS: - ESWAT + -        \n\r\n\r" \
        "GREETZ TO : AVB,NEXT,AUTOMATION,TMB     \n\r" \
        "THE POMPEY PIRATES,PHALANX,ALLIANCE,BB, \n\r" \
        "DELIGHT,FOF,TLB,TDI,QUARTET,HTL,MCA,    \n\r" \
        "THOR,MAD VISION,INNER CIRCLE,TSG...     \n\r\n\r" \
        "MEGA THANX TO THE BWK FOR DIS LOGO...   \n\r\n\r" \
        "          BASH 'T' FOR TRAINER          \n\r" \
    };

    xpad = 0;
    ypad = 0;
    //getFonts(fonts, screen, 0, 0, 8, 8, 320, 200, 89);
    length = strlen(scrollText);
    for (i = 0 ; i < length ; i++)
    {
        char c = scrollText[i];
        if (c == '\n')
        {
            ypad+=8;
            continue;
        }
        if (c == '\r')
        {
            xpad = 0;
            continue;
        }

        xpad +=8;

        getFonts(fonts, screen, xpad, ypad+68, 8, 8, getLetterPos(c));
    }


#if TEST
    getFonts(fonts, screen, 0 , 0, 8, 8, getLetterPos('¸'));
    getFonts(fonts, screen, 0 , 8, 8, 8, getLetterPos('‰'));
#endif
}



static SDL_Surface *ChangeVideoMode(int w, int h, int bpp, int flags)
{
    return SDL_SetVideoMode(w, h, bpp, flags);
}

static void HandleEvent(SDL_Event *event)
{
    switch(event->type)
    {
        case SDL_KEYDOWN:
        {
            if ( event->key.keysym.sym == SDLK_ESCAPE )
                fin_prg = TRUE;
            if ( (event->key.keysym.sym == SDLK_SPACE || event->key.keysym.sym == SDLK_t) && fin_scene1)
                fin_anim = TRUE;
            break;
        }
           
        case SDL_QUIT:
        {
            fin_prg = TRUE;
            break;
        }
    }
}

int main(int argc, char *argv[])
{
    SDL_Event event;
    SDL_Surface *screen;
    SDL_Surface *fonts;
    SDL_RWops *fontsRW;
    SDL_Surface *logo;
    SDL_RWops *logoRW;
    SDL_Surface *prefinalScreen; /* pre render buffer */
    SDL_Surface *finalScreen;      /* buffer before display screen */
    SDL_Rect dst;
    Uint32 ticksOld;
    Uint32 ticks;


    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("Unable to init SDL : %s\n", SDL_GetError());
        return -1;
    }

    atexit(SDL_Quit);

   

#if defined(_MSC_VER) || defined(_WIN32)

    FullScreen = MessageBox((HWND)NULL, TEXT("Fullscreen ?"), TEXT("Full or Not ? :=)"), MB_YESNO);

    screen = ChangeVideoMode(WIN_HEIGHT, WIN_WIDTH, WIN_BPP,
        (FullScreen == IDYES) ? SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_FULLSCREEN:
                                 SDL_DOUBLEBUF | SDL_HWSURFACE);

    if (screen == (SDL_Surface *)NULL)
#else
    screen = ChangeVideoMode(WIN_HEIGHT, WIN_WIDTH, WIN_BPP, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_FULLSCREEN);
    if (screen == (SDL_Surface *)NULL)
#endif
    {
         printf("Unable to open video mode : %s\n",SDL_GetError());
         exit(EXIT_FAILURE);
    }

    SDL_WM_SetCaption(INTRONAME, (const char *)NULL);
    SDL_ShowCursor(SDL_DISABLE);
    fontsRW = SDL_RWFromMem(eswatFonts, sizeof(eswatFonts)/sizeof(eswatFonts[0]));
    fonts = SDL_LoadBMP_RW(fontsRW, 1);
    assert(fonts != (SDL_Surface *) NULL);
    logoRW = SDL_RWFromMem(eswatLogo, sizeof(eswatLogo)/sizeof(eswatLogo[0]));
    logo = SDL_LoadBMP_RW(logoRW, 1);
    /* create the last buffer which used before display screen */
    finalScreen = SDL_CreateRGBSurface(0, WIN_WIDTH, WIN_HEIGHT, WIN_BPP, 0, 0, 0, 0);
    /* pre render buffer */
    prefinalScreen = SDL_CreateRGBaSurface(0, WIN_WIDTH, WIN_HEIGHT, WIN_BPP, 0, 0, 0, 0);

    /* print logo on pre render buffer */
    SDL_BlitSurface(logo, NULL, prefinalScreen, NULL);
    /* same thing for fonts */
    PrintPayload(prefinalScreen, fonts);

    /* dst represent fonts blitting rectangle x,y,w,h*/
    dst.x = 0;
    dst.y = WIN_HEIGHT;
    dst.w = WIN_WIDTH;
    dst.h = WIN_HEIGHT;
    ticksOld = SDL_GetTicks();
    while (!fin_prg)
    {
        /* update/render Anim opengl */
        while (SDL_PollEvent(&event))
        {
            HandleEvent(&event);
        }

        ticks = SDL_GetTicks();
        if (ticks - ticksOld > (1/60))
        {
            ticksOld = ticks;
            SDL_FillRect(finalScreen, NULL, 0); /* reset background buffer before display with black color */
            if (!fin_anim) {
                if (dst.y > 0) {
                    dst.y -= 4;    /* move the pre render buffer y axis of -4 pixels*/
                } else {
                    fin_scene1 = TRUE;
                }
            } else {
                if (dst.y <= WIN_HEIGHT) {
                  dst.y += 4;
                } else {
                  fin_prg = TRUE;
                }
            }
        }
        SDL_BlitSurface(prefinalScreen, NULL, finalScreen, &dst);  // Here I print the logos/fonts at dst.y position until 0.
        SDL_BlitSurface(finalScreen, NULL, screen, NULL);            // Here i copy the finalbuffer on the screen
        SDL_Flip(screen);                                                             // flipbuffers() like

    }

    SDL_FreeSurface(fonts);

    SDL_FreeSurface(prefinalScreen);
    SDL_FreeSurface(finalScreen);
    SDL_FreeSurface(screen);

    SDL_ShowCursor(SDL_ENABLE);
    SDL_Quit();


    return 0;
}

Voila, my objective will be to have a same thing like prefinalscreen and finalscreen in purebasic semantic with Image ? or Sprite ?
Title: Re: Requiring helps for purebasic.
Post by: padman on August 03, 2011
Ok, I could be terribly wrong here (I'm just a lousy BASIC coder ::)), but you are basically creating a front/backbuffer on your own, flipping those two then right? (Btw is SDL OpenGL based or is it DirectX?) PB uses an inbuilt double buffering method. I think you should just go away from converting your SDL stuff 1:1 to PB (it's not gonna work) and not complicate things. Try the easy approaches shown above. ;)
Any other opinions/ideas/explanations on this are very welcome! Come on guys!
Title: Re: Requiring helps for purebasic.
Post by: Jim on August 03, 2011
I'm guessing he wants to render offscreen, then apply some post-processing pixel-wise over the top.  I'm no PB expert so I don't know how to do that.

Jim
Title: Re: Requiring helps for purebasic.
Post by: padman on August 03, 2011
Erm okay. Thanks! That's like taking a sledgehammer to crack a nut for creating a simple effect like this scroller in PB. (and btw I have no idea at the moment how to do that either or maybe I'm just not getting it...)

 
Title: Re: Requiring helps for purebasic.
Post by: jace_stknights on August 03, 2011
My Dear Maracuja  :buddies:,

I don't understand why you say Purebasic is different of SDL?!? You are using exactly the "same" things with it!

    SDL_BlitSurface(fonts, &src, screen, &dst); -> DisplaySprite(0, sco+cco, 320)
    SDL_FillRect(finalScreen, NULL, 0);  -> Clearscreen(0)

    SDL_BlitSurface(prefinalScreen, NULL, finalScreen, &dst);  // Here I print the logos/fonts at dst.y position until 0.
    SDL_BlitSurface(finalScreen, NULL, screen, NULL);            // Here i copy the finalbuffer on the screen
    SDL_Flip(screen);                                                             // flipbuffers() like

    -> Flipbuffers() yep, the switch between logical and physical screen are completely transparent!

So you just have to work onto the logical screen (ScreenOutput()) and display it after all jobs....

But to be back at your question, I think you want to know how to make a scoll text in a buffer to display it everywhere onto screen.

I've sent you the STCS remake code! It is using a "pre-buffered" sprite like you want! I create a sprite first, then I display all the font needed inside, and then i display the sprite everywhere on the screen - I must admit - using DisplayTransparentSprite or Peek & Poke (Hi PadMan!)

The trick is: you can't sprite into a sprite! So you have 3 options:
1- using images, and DrawImage() and displaying them into the sprite.
2- using your self-sprite rout with Peek & Poke and working in byte mode in the sprite (see STCS remake)!
3- display onto the screen at start of frame, grabbing the part of the screen in a sprite, clearing the screen, displaying all the other effect, and then displaying the sprite you created at first onto the rest!!! and at the end flipbuffers()!

Here is the version using the third technique (based on the last version of Padman):

Code: [Select]
Global s$,s1$
; Constantes

Global sco=0
Global tptr=1


#WINTITLE  = "E-Swat - Replicants Remake"
#WINHEIGHT = 480 ; Impossible de faire du 320x200, résolution minimale 640x480
#WINWIDTH  = 640
#BPP          = 32

s1$="                        I'M SUPPOSED TO BE A SCROLLTEXT SCROLLING HAPPILY OVER THE SCREEN                                             "


;Declaration procedure
Declare getLetterPos(caractere$)
Declare getFonts(IdSprite, x, y, w, h, num, r, g, b)
Declare ErrorWindowCreation()
Declare Anim(fullscreen.b)
Declare PrintTextWindows(text$)

;Include text
XIncludeFile "Payload.pbi" ; crack message


; give the position of caractere$ in alpha variable.
; parameters:
; In :
; - caractere$ : a char to find in a array which synchronized with letter on a bitmap fonts
; Out :
; - an integer which returns the good position if the char exist else 0 but here 0 is space.
Procedure getLetterPos(caractere$)

  alpha$ =          " !" + Chr(34) + "    '()*+,-. 0123456789:; = ? ABCDEFG"
  alpha$ = alpha$ + "HIJKLMNOPQRSTUVWXYZ"
 

    length = Len(alpha$);

    i = 1;

    While (Mid(alpha$, i, 1) <> Mid(caractere$, 1, 1) And i <= length)
      i = i + 1
    Wend

    If i > length
      ProcedureReturn 0
    EndIf
   
    ProcedureReturn i; 
EndProcedure


; take the good bitmap char in a sprite picture. Before num computed by getLetterPos
; parameters :
; In :
; - IdSprite : sprite handle which contains bitmap fonts
; - x,y : letter position in pixel in a pic
; - w,h : width, height in pixel for a letter
; - num : logic letter position in a pic
; - r,g,b : transparency color for sprite
; Out:
;
Procedure getFonts(IdSprite, x, y, w, h, num, r, g, b)
  fontsW = SpriteWidth(IdSprite) ; [!] Just take with sprite width
  ; ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
  ; reinit original sprite size after clipping
  posX = (num-1) % ((fontsW / w))
  posY = (num-1) / ((fontsW / w))
  srcX = (posX) * w
  srcY = (posY) * h
  srcH = h
  srcW = w
 
  ClipSprite(IdSprite, srcX, srcY, srcW, srcH)
  TransparentSpriteColor(IdSprite, RGB(r,g,b))
  DisplayTransparentSprite(IdSprite, x, y)
  ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
EndProcedure

; Generic Error function.
Procedure ErrorWindowCreation()
  MessageRequester("Error", "This is fucked up, I can't create the window", 0)
  End
EndProcedure

; Animatation routine
; In:
; - fullscreen.b : create an animation in fullscreen mode or not. Two state: 0/false -> windowed mode, 1/true -> fullscreen
: Out:
;
Procedure Anim(fullscreen.b)
  ;Init subsystem and the windows with fullscreen or not
  InitSprite()
  InitKeyboard()
  If fullscreen
    If OpenScreen(#WINWIDTH, #WINHEIGHT, #BPP, #WINTITLE) = 0
      ErrorWindowCreation()
    EndIf
  Else
    If OpenWindow(1337,0,0,#WINWIDTH, #WINHEIGHT, #WINTITLE, #PB_Window_BorderLess | #PB_Window_ScreenCentered)
      If Not OpenWindowedScreen(WindowID(1337), 0, 0, #WINWIDTH, #WINHEIGHT, 1, 0, 0)
        ErrorWindowCreation()
      EndIf
    Else
       ErrorWindowCreation()
    EndIf
  EndIf

  ;Animation
  CatchSprite(0, ?fontsReps, #PB_Sprite_Texture)
 
 
 
 ;####################################################################

 Repeat 
   
   
   ; we are working in the LOGICAL screen, not displayed!!!
   
   ; we display the scrolltext at line 0
    cco = 0
 
    For cc = 0 To 25                                                    ; change to higher value to start further right
      letter   = Asc(Mid(s1$, tptr+cc, 1))-31                            ;your font is in ascii order, no need for special lookup index
   
 
     
   
      yCharPos = letter / 40
      xCharPos = letter % 40
     
        If xcharpos= 0: xcharpos = 40 : ycharpos=yCharPos-1  :EndIf
      If xCharPos <- 1: xCharPos = 39
   
      ElseIf xCharPos > 0  : xCharPos = xCharPos - 1
      EndIf
     
     
      ClipSprite(0, xCharPos*8, yCharPos*8, 8, 8)
      DisplaySprite(0, sco+cco, 0)
      cco = cco + 8
    Next
   
 
    sco = sco -2                                 ;change scrollspeed here
  ;     
    If sco < -8
      tptr = tptr + 1
      sco = sco + 8
    EndIf
  ;     
    If tptr > Len(s1$)-31
      tptr = 1
    EndIf
   
   
    ; we grab the sprite from the screen (so the screen was used as buffer!!!)
   
    GrabSprite(12,0,0,320,8)
   
    ; now we make like a normal frame, everything drawed after this will be dispayed!
   
    ClearScreen(0)
   
    xpad = 0
    ypad = 0
 
    For i = 1 To Len(s$) Step 1
      car$ = Mid(s$, i , 1)
      Select car$
        Case Chr(10)
          ypad = ypad + 8
        Case Chr(13)
          xpad = 0
        Default
          getFonts(0,xpad, ypad+156, 8, 8, getLetterPos(car$), 0, 0, 0)
          xpad = xpad + 8
      EndSelect
    Next i
   
   
    ; at least I display the sprite created from the "buffer" (screen) onto the rest!
    TransparentSpriteColor(12,RGB(0,0,0))
    DisplayTransparentSprite(12,0,200+50*Sin(Radian(ang)))
    ang = ang + 2
    If ang > 360
      ang = 0
    EndIf

   

   

   ;##########################################################################
   
 
    FlipBuffers()

    If fullscreen = 0
      Event = WaitWindowEvent(0)
    EndIf
    ExamineKeyboard()
  Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Space) Or KeyboardPushed(#PB_Key_T)
 
EndProcedure

; Init the question for fullscreen or windowed mode. After selection the button, the animation will be in a good screen mode
; In :
; - text$ : Is the message for MessageBox
; Out :
;
Procedure PrintTextWindows(text$)
  Result = MessageRequester(#WINTITLE, text$,#PB_MessageRequester_YesNoCancel)
  Select Result
    Case #PB_MessageRequester_Yes
      Anim(#True)
    Case #PB_MessageRequester_No
      Anim(#False)
    EndSelect
EndProcedure



;!!!!! Begin !!!!!
  PrintTextWindows("Credits : Jace, Padman, StormBringer !!! and you ;o)"+ Chr(10) + Chr(13) + "Do you want Fullscreen ? :)")

  End ; va!n advice
;!!!!! End !!!!!

DataSection
  fontsReps: IncludeBinary("eswatFonts.bmp")
  logoReps:  IncludeBinary("eswatLogo.bmp")
EndDataSection


As you can see, you are using a buffer :D :D :D

Next post, the first way!


Title: Re: Requiring helps for purebasic.
Post by: padman on August 03, 2011
Ah thanks Jace, so he wanted something like the GrabSprite thing you showed me once. :inspired:  Thanks for clearing that. Could have thought of it myself. D'oh.
(Nevertheless, I still like my solution. hehe)
Title: Re: Requiring helps for purebasic.
Post by: jace_stknights on August 03, 2011
Hehe, your solution stays the best for displaying "normal" scrolltext (I'm still using it  ;D) But for some effects, i think buffering allows more things!

And here is the first method:

some parts are not used anymore...

Code: [Select]
Global s$,s1$
; Constantes

Global sco=0
Global tptr=1


#WINTITLE  = "E-Swat - Replicants Remake"
#WINHEIGHT = 480 ; Impossible de faire du 320x200, résolution minimale 640x480
#WINWIDTH  = 640
#BPP          = 32

s1$="                                 I'M SUPPOSED TO BE A SCROLLTEXT SCROLLING HAPPILY OVER THE SCREEN                                                                                     "


;Declaration procedure
Declare getLetterPos(caractere$)
Declare getFonts(IdSprite, x, y, w, h, num, r, g, b)
Declare ErrorWindowCreation()
Declare Anim(fullscreen.b)
Declare PrintTextWindows(text$)

;Include text
XIncludeFile "Payload.pbi" ; crack message


; give the position of caractere$ in alpha variable.
; parameters:
; In :
; - caractere$ : a char to find in a array which synchronized with letter on a bitmap fonts
; Out :
; - an integer which returns the good position if the char exist else 0 but here 0 is space.
Procedure getLetterPos(caractere$)

  alpha$ =          " !" + Chr(34) + "    '()*+,-. 0123456789:; = ? ABCDEFG"
  alpha$ = alpha$ + "HIJKLMNOPQRSTUVWXYZ"
 

    length = Len(alpha$);

    i = 1;

    While (Mid(alpha$, i, 1) <> Mid(caractere$, 1, 1) And i <= length)
      i = i + 1
    Wend

    If i > length
      ProcedureReturn 0
    EndIf
   
    ProcedureReturn i; 
EndProcedure


; take the good bitmap char in a sprite picture. Before num computed by getLetterPos
; parameters :
; In :
; - IdSprite : sprite handle which contains bitmap fonts
; - x,y : letter position in pixel in a pic
; - w,h : width, height in pixel for a letter
; - num : logic letter position in a pic
; - r,g,b : transparency color for sprite
; Out:
;
Procedure getFonts(IdSprite, x, y, w, h, num, r, g, b)
  fontsW = SpriteWidth(IdSprite) ; [!] Just take with sprite width
  ; ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
  ; reinit original sprite size after clipping
  posX = (num-1) % ((fontsW / w))
  posY = (num-1) / ((fontsW / w))
  srcX = (posX) * w
  srcY = (posY) * h
  srcH = h
  srcW = w
 
  ClipSprite(IdSprite, srcX, srcY, srcW, srcH)
  TransparentSpriteColor(IdSprite, RGB(r,g,b))
  DisplayTransparentSprite(IdSprite, x, y)
  ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
EndProcedure

; Generic Error function.
Procedure ErrorWindowCreation()
  MessageRequester("Error", "This is fucked up, I can't create the window", 0)
  End
EndProcedure

; Animatation routine
; In:
; - fullscreen.b : create an animation in fullscreen mode or not. Two state: 0/false -> windowed mode, 1/true -> fullscreen
: Out:
;
Procedure Anim(fullscreen.b)
  ;Init subsystem and the windows with fullscreen or not
  InitSprite()
  InitKeyboard()
  If fullscreen
    If OpenScreen(#WINWIDTH, #WINHEIGHT, #BPP, #WINTITLE) = 0
      ErrorWindowCreation()
    EndIf
  Else
    If OpenWindow(1337,0,0,#WINWIDTH, #WINHEIGHT, #WINTITLE, #PB_Window_BorderLess | #PB_Window_ScreenCentered)
      If Not OpenWindowedScreen(WindowID(1337), 0, 0, #WINWIDTH, #WINHEIGHT, 1, 0, 0)
        ErrorWindowCreation()
      EndIf
    Else
       ErrorWindowCreation()
    EndIf
  EndIf

  ;Animation
  ;CatchSprite(0, ?fontsReps, #PB_Sprite_Texture) ; we don't use the fonts as sprite!
 
  Dim charList(60) ; array with all images ID
  font = CatchImage (#PB_Any, ?fontsReps)   
  xp = 0
  yp = 0
  For loop =0 To 59                               
    charList(loop) = GrabImage(font,#PB_Any,xp,yp,8,8)   ; create a new image for each font!
    xp = xp + 8                                         
    If xp > = 320
      yp = yp + 8
      xp = 0
    EndIf
  Next
  ; now we got all the font ready "inside" an array...
 
  CreateSprite(1,320,8)  ; sprite used as buffer
 
 ;####################################################################

 Repeat 
   
    StartDrawing (SpriteOutput(1))  ; select the sprite 1 (buffer) as the display output

    cco = 0
 
    For cc = 0 To 40
      letter   = Asc(Mid(s1$, tptr+cc, 1))-32 ; get the font pos in the array (0 is space, and so on)
      DrawImage(ImageID(charList(letter)),sco+cco, 0) ; display the font into the sprite! You have sometimes to use ImageID(), sometimes no, dunno why ...
      cco = cco + 8
    Next
    StopDrawing() ; end of drawing in sprite 1   
   
 
    sco = sco -2                                 ;change scrollspeed here
  ;     
    If sco < -8
      tptr = tptr + 1
      sco = sco + 8
    EndIf
  ;     
    If tptr > Len(s1$) - 41 ; take care of this: the value must be equal to the number of char displayed!
      tptr = 1
    EndIf
   
     
    ClearScreen(0)
   
    StartDrawing(ScreenOutput())
    TransparentSpriteColor(1,RGB(0,0,0))
    DisplayTransparentSprite(1,0,200+50*Sin(Radian(ang)))  ;display the sprite (buffer) onto the screen!
    StopDrawing()
    ang = ang + 2
    If ang > 360
      ang = 0
    EndIf

    FlipBuffers()

    If fullscreen = 0
      Event = WaitWindowEvent(0)
    EndIf
    ExamineKeyboard()
  Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Space) Or KeyboardPushed(#PB_Key_T)
 
EndProcedure

; Init the question for fullscreen or windowed mode. After selection the button, the animation will be in a good screen mode
; In :
; - text$ : Is the message for MessageBox
; Out :
;
Procedure PrintTextWindows(text$)
  Result = MessageRequester(#WINTITLE, text$,#PB_MessageRequester_YesNoCancel)
  Select Result
    Case #PB_MessageRequester_Yes
      Anim(#True)
    Case #PB_MessageRequester_No
      Anim(#False)
    EndSelect
EndProcedure



;!!!!! Begin !!!!!
  PrintTextWindows("Credits : Jace, Padman, StormBringer !!! and you ;o)"+ Chr(10) + Chr(13) + "Do you want Fullscreen ? :)")

  End ; va!n advice
;!!!!! End !!!!!

DataSection
  fontsReps: IncludeBinary("eswatFonts.bmp")
  logoReps:  IncludeBinary("eswatLogo.bmp")
EndDataSection
Title: Re: Requiring helps for purebasic.
Post by: padman on August 03, 2011
Quote
But for some effects, i think buffering allows more things!
You are absolutely right. Some effects aren't even possible w/o your techniques. But he only wants to display a white font...  :P
Anyway have some extra Karma for this version  :goodpost:
Title: Re: Requiring helps for purebasic.
Post by: jace_stknights on August 03, 2011
  :cheers:
Title: Re: Requiring helps for purebasic.
Post by: maracuja on August 03, 2011
Hello !

Ok, I could be terribly wrong here (I'm just a lousy BASIC coder ::)), but you are basically creating a front/backbuffer on your own, flipping those two then right? (Btw is SDL OpenGL based or is it DirectX?) PB uses an inbuilt double buffering method. I think you should just go away from converting your SDL stuff 1:1 to PB (it's not gonna work) and not complicate things. Try the easy approaches shown above. ;)
Any other opinions/ideas/explanations on this are very welcome! Come on guys!

I promise to not paste here some C code in this section. :) It's just for illustrate my talk. :)
Yes, you're understand, i'm creating front/backbuffer and flipping where all is ok. :) (Here, it's just SDL mechanism but with DirectX (DirectDraw is same). Yeah, i would like to follow your advice but i'm lost because i haven't these pb reflex. :) To say true, i coded in "c/sdl version very quickly" but not in pb. I taked some effort for worse result in my case.

My Dear Maracuja  :buddies:,

I don't understand why you say Purebasic is different of SDL?!? You are using exactly the "same" things with it!

    SDL_BlitSurface(fonts, &src, screen, &dst); -> DisplaySprite(0, sco+cco, 320)
    SDL_FillRect(finalScreen, NULL, 0);  -> Clearscreen(0)

    SDL_BlitSurface(prefinalScreen, NULL, finalScreen, &dst);  // Here I print the logos/fonts at dst.y position until 0.
    SDL_BlitSurface(finalScreen, NULL, screen, NULL);            // Here i copy the finalbuffer on the screen
    SDL_Flip(screen);                                                             // flipbuffers() like

    -> Flipbuffers() yep, the switch between logical and physical screen are completely transparent!

So you just have to work onto the logical screen (ScreenOutput()) and display it after all jobs....

But to be back at your question, I think you want to know how to make a scoll text in a buffer to display it everywhere onto screen.

I've sent you the STCS remake code! It is using a "pre-buffered" sprite like you want! I create a sprite first, then I display all the font needed inside, and then i display the sprite everywhere on the screen - I must admit - using DisplayTransparentSprite or Peek & Poke (Hi PadMan!)

The trick is: you can't sprite into a sprite! So you have 3 options:
1- using images, and DrawImage() and displaying them into the sprite.
2- using your self-sprite rout with Peek & Poke and working in byte mode in the sprite (see STCS remake)!
3- display onto the screen at start of frame, grabbing the part of the screen in a sprite, clearing the screen, displaying all the other effect, and then displaying the sprite you created at first onto the rest!!! and at the end flipbuffers()!

Here is the version using the third technique (based on the last version of Padman):

... code cut ...

As you can see, you are using a buffer :D :D :D

Next post, the first way!

Dear Jace/St Knights ! ;-)

I read your "c/sdl->pb cheatsheet", and I'm undertanding in theory these code matching but now i will try later. :)
Title: Re: Requiring helps for purebasic.
Post by: maracuja on August 03, 2011
I'm back, so this code in "theory" will be work but not....
Code: [Select]
Global s$
; Constants

#WINTITLE  = "E-Swat - Replicants Remake"
#WINHEIGHT = 480
#WINWIDTH  = 640
#BPP       = 32
#SPRH = 16 ; sprite height
#SPRW = 16;  " "    width
;Declaration procedure
Declare PrintTransparentSprite(IdSprite, x, y , r, g, b)
Declare getLetterPos(caractere$)
Declare getFonts(IdSprite, x, y, w, h, num, r, g, b)
Declare ErrorWindowCreation()
Declare Anim(fullscreen.b)
Declare PrintTextWindows(text$)
;
; 8x8
; DC.B      '            ----------------            '
; DC.B      '            -THE REPLICANTS-            '
; DC.B      '            ----------------            ',$00
; DC.B      '           PRESENTS: - ESWAT + -        '
; DC.B      '                                        '
; DC.B      'GREETZ TO : AVB,NEXT,AUTOMATION,TMB     '
; DC.B      'THE POMPEY PIRATES,PHALANX,ALLIANCE,BB, '
; DC.B      'DELIGHT,FOF,TLB,TDI,QUARTET,HTL,MCA,    '
; DC.B      'THOR,MAD VISION,INNER CIRCLE,TSG...     ',$00
; DC.B      '                                        '
; DC.B      'MEGA THANX TO THE BWK FOR DIS LOGO...   ',$00
; DC.B      '          BASH ',$27,'T',$27,' FOR TRAINER          ',$FF
;
s$ =      "            ----------------            " + Chr(10) + Chr(13)
s$ = s$ + "            -THE REPLICANTS-            " + Chr(10) + Chr(13)
s$ = s$ + "            ----------------            " + Chr(10) + Chr(13) + Chr(10) + Chr(13)
s$ = s$ + "           PRESENTS: - ESWAT + -        " + Chr(10) + Chr(13) + Chr(10) + Chr(13)
s$ = s$ + "GREETZ TO : AVB,NEXT,AUTOMATION,TMB     " + Chr(10) + Chr(13)
s$ = s$ + "THE POMPEY PIRATES,PHALANX,ALLIANCE,BB, " + Chr(10) + Chr(13)
s$ = s$ + "DELIGHT,FOF,TLB,TDI,QUARTET,HTL,MCA,    " + Chr(10) + Chr(13)
s$ = s$ + "THOR,MAD VISION,INNER CIRCLE,TSG...     " + Chr(10) + Chr(13) + Chr(10) + Chr(13)
s$ = s$ + "MEGA THANX TO THE BWK FOR DIS LOGO...   " + Chr(10) + Chr(13) + Chr(10) + Chr(13)
s$ = s$ + "          BASH 'T' FOR TRAINER          " + Chr(10) + Chr(13)


; print a sprites
Procedure PrintTransparentSprite(IdSprite, x, y, r, g, b)
  TransparentSpriteColor(IdSprite, RGB(r,g,b))
  DisplayTransparentSprite(IdSprite, x, y)
EndProcedure

; give the position of caractere$ in alpha variable.
; parameters:
; In :
; - caractere$ : a char to find in a array which synchronized with letter on a bitmap fonts
; Out :
; - an integer which returns the good position if the char exist else 0 but here 0 is space.
Procedure getLetterPos(caractere$)

  alpha$ =          " !" + Chr(34) + "    '()*+,-. 0123456789:; = ? ABCDEFG"
  alpha$ = alpha$ + "HIJKLMNOPQRSTUVWXYZ"
 

    length = Len(alpha$);

    i = 1;

    While (Mid(alpha$, i, 1) <> Mid(caractere$, 1, 1) And i <= length)
      i = i + 1
    Wend

    If i > length
      ProcedureReturn 0
    EndIf
   
    ProcedureReturn i; 
EndProcedure


; take the good bitmap char in a sprite picture. Before num computed by getLetterPos
; parameters :
; In :
; - IdSprite : sprite handle which contains bitmap fonts
; - x,y : letter position in pixel in a pic
; - w,h : width, height in pixel for a letter
; - num : logic letter position in a pic
; - r,g,b : transparency color for sprite
; Out:
;
Procedure getFonts(IdSprite, x, y, w, h, num, r, g, b)
  fontsW = SpriteWidth(IdSprite) ; [!] Just take with sprite width
  ; ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
  ; reinit original sprite size after clipping
  posX = (num-1) % ((fontsW / w))
  posY = (num-1) / ((fontsW / w))
  srcX = (posX) * w
  srcY = (posY) * h
  srcH = h
  srcW = w
 
  ClipSprite(IdSprite, srcX, srcY, srcW, srcH)
  PrintTransparentSprite(IdSprite, x, y, r, g, b)
  ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
EndProcedure

; Generic Error function.
Procedure ErrorWindowCreation()
  MessageRequester("Error", "This is fucked up, I can't create the window", 0)
  End
EndProcedure

; Animatation routine
; In:
; - fullscreen.b : create an animation in fullscreen mode or not. Two state: 0/false -> windowed mode, 1/true -> fullscreen
: Out:
;
Procedure Anim(fullscreen.b)
  ;Init subsystem and the windows with fullscreen or not
  InitSprite()
  InitKeyboard()
  If fullscreen
    If OpenScreen(#WINWIDTH, #WINHEIGHT, #BPP, #WINTITLE) = 0
      ErrorWindowCreation()
    EndIf
  Else
    If OpenWindow(1337,0,0,#WINWIDTH, #WINHEIGHT, #WINTITLE, #PB_Window_BorderLess | #PB_Window_ScreenCentered)
      If Not OpenWindowedScreen(WindowID(1337), 0, 0, #WINWIDTH, #WINHEIGHT, 1, 0, 0)
        ErrorWindowCreation()
      EndIf
    Else
       ErrorWindowCreation()
    EndIf
  EndIf

  ;Animation
  Result = CatchSprite(0, ?fontsReps, #PB_Sprite_Texture)
  Result = CatchSprite(1, ?logoReps, #PB_Sprite_Texture)
  Result = CreateImage(12, #WINWIDTH, #WINHEIGHT,#BPP)
  ClearScreen(0)
  Repeat
  xpad = 0
  ypad = 0
  StartDrawing(ImageOutput(12))
  For i = 1 To Len(s$) Step 1
    car$ = Mid(s$, i , 1)
    Select car$
      Case Chr(10)
        ypad = ypad + #SPRH
      Case Chr(13)
        xpad = 0
      Default
        getFonts(0,xpad, ypad+156, #SPRW, #SPRH, getLetterPos(car$), 0, 0, 0)
        xpad = xpad + #SPRW
    EndSelect
  Next i
  PrintTransparentSprite(1, 0, 0, 0, 0, 0)
  StopDrawing()
   
   
  ClearScreen(0)
  StartDrawing(ScreenOutput())
  DrawImage(ImageID(12),0,100) ; Here, if i removed ImageID the program segfault.
  StopDrawing()                              ; In all case nothing are print. But it's backbuffer -> frontbuffer right ?
  FlipBuffers()
    If fullscreen = 0
      Event = WaitWindowEvent(0)
    EndIf
    ExamineKeyboard()
  Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Space) Or KeyboardPushed(#PB_Key_T)
 
EndProcedure

; Init the question for fullscreen or windowed mode. After selection the button, the animation will be in a good screen mode
; In :
; - text$ : Is the message for MessageBox
; Out :
;
Procedure PrintTextWindows(text$)
  Result = MessageRequester(#WINTITLE, text$,#PB_MessageRequester_YesNoCancel)
  Select Result
    Case #PB_MessageRequester_Yes
      Anim(#True)
    Case #PB_MessageRequester_No
      Anim(#False)
    EndSelect
EndProcedure



;!!!!! Begin !!!!!
  PrintTextWindows("Credits : Jace, Padman, StormBringer !!! and you ;o)"+ Chr(10) + Chr(13) + "Do you want Fullscreen ? :)")

  End ; va!n advice
;!!!!! End !!!!!

DataSection
  fontsReps: IncludeBinary("eswatfonts.bmp")
  logoReps:  IncludeBinary("eswatlogo.bmp")
EndDataSection

; IDE Options = PureBasic 4.51 (Linux - x86)
; CursorPosition = 196
; FirstLine = 158
; Folding = -
Title: Re: Requiring helps for purebasic.
Post by: maracuja on August 03, 2011
Another strange effect :

Code: [Select]
Global s$
; Constantes

#WINTITLE  = "E-Swat - Replicants Remake"
#WINHEIGHT = 480 ; Impossible de faire du 320x200, résolution minimale 640x480
#WINWIDTH  = 640
#BPP       = 32
#SPRH = 16
#SPRW = 16
;Declaration procedure
Declare PrintTransparentSprite(IdSprite, x, y , r, g, b)
Declare getLetterPos(caractere$)
Declare getFonts(IdSprite, x, y, w, h, num, r, g, b)
Declare ErrorWindowCreation()
Declare Anim(fullscreen.b)
Declare PrintTextWindows(text$)

;Include text
;XIncludeFile "Payload.pbi" ; crack message

;
; 8x8
; DC.B      '            ----------------            '
; DC.B      '            -THE REPLICANTS-            '
; DC.B      '            ----------------            ',$00
; DC.B      '           PRESENTS: - ESWAT + -        '
; DC.B      '                                        '
; DC.B      'GREETZ TO : AVB,NEXT,AUTOMATION,TMB     '
; DC.B      'THE POMPEY PIRATES,PHALANX,ALLIANCE,BB, '
; DC.B      'DELIGHT,FOF,TLB,TDI,QUARTET,HTL,MCA,    '
; DC.B      'THOR,MAD VISION,INNER CIRCLE,TSG...     ',$00
; DC.B      '                                        '
; DC.B      'MEGA THANX TO THE BWK FOR DIS LOGO...   ',$00
; DC.B      '          BASH ',$27,'T',$27,' FOR TRAINER          ',$FF
;
s$ =      "            ----------------            " + Chr(10) + Chr(13)
s$ = s$ + "            -THE REPLICANTS-            " + Chr(10) + Chr(13)
s$ = s$ + "            ----------------            " + Chr(10) + Chr(13) + Chr(10) + Chr(13)
s$ = s$ + "           PRESENTS: - ESWAT + -        " + Chr(10) + Chr(13) + Chr(10) + Chr(13)
s$ = s$ + "GREETZ TO : AVB,NEXT,AUTOMATION,TMB     " + Chr(10) + Chr(13)
s$ = s$ + "THE POMPEY PIRATES,PHALANX,ALLIANCE,BB, " + Chr(10) + Chr(13)
s$ = s$ + "DELIGHT,FOF,TLB,TDI,QUARTET,HTL,MCA,    " + Chr(10) + Chr(13)
s$ = s$ + "THOR,MAD VISION,INNER CIRCLE,TSG...     " + Chr(10) + Chr(13) + Chr(10) + Chr(13)
s$ = s$ + "MEGA THANX TO THE BWK FOR DIS LOGO...   " + Chr(10) + Chr(13) + Chr(10) + Chr(13)
s$ = s$ + "          BASH 'T' FOR TRAINER          " + Chr(10) + Chr(13)


; print a sprites
Procedure PrintTransparentSprite(IdSprite, x, y, r, g, b)
  TransparentSpriteColor(IdSprite, RGB(r,g,b))
  DisplayTransparentSprite(IdSprite, x, y)
EndProcedure

; give the position of caractere$ in alpha variable.
; parameters:
; In :
; - caractere$ : a char to find in a array which synchronized with letter on a bitmap fonts
; Out :
; - an integer which returns the good position if the char exist else 0 but here 0 is space.
Procedure getLetterPos(caractere$)

  alpha$ =          " !" + Chr(34) + "    '()*+,-. 0123456789:; = ? ABCDEFG"
  alpha$ = alpha$ + "HIJKLMNOPQRSTUVWXYZ"
 

    length = Len(alpha$);

    i = 1;

    While (Mid(alpha$, i, 1) <> Mid(caractere$, 1, 1) And i <= length)
      i = i + 1
    Wend

    If i > length
      ProcedureReturn 0
    EndIf
   
    ProcedureReturn i; 
EndProcedure


; take the good bitmap char in a sprite picture. Before num computed by getLetterPos
; parameters :
; In :
; - IdSprite : sprite handle which contains bitmap fonts
; - x,y : letter position in pixel in a pic
; - w,h : width, height in pixel for a letter
; - num : logic letter position in a pic
; - r,g,b : transparency color for sprite
; Out:
;
Procedure getFonts(IdSprite, x, y, w, h, num, r, g, b)
  fontsW = SpriteWidth(IdSprite) ; [!] Just take with sprite width
  ; ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
  ; reinit original sprite size after clipping
  posX = (num-1) % ((fontsW / w))
  posY = (num-1) / ((fontsW / w))
  srcX = (posX) * w
  srcY = (posY) * h
  srcH = h
  srcW = w
 
  ClipSprite(IdSprite, srcX, srcY, srcW, srcH)
  PrintTransparentSprite(IdSprite, x, y, r, g, b)
  ClipSprite(IdSprite, #PB_Default, #PB_Default, #PB_Default, #PB_Default)
EndProcedure

; Generic Error function.
Procedure ErrorWindowCreation()
  MessageRequester("Error", "This is fucked up, I can't create the window", 0)
  End
EndProcedure

; Animatation routine
; In:
; - fullscreen.b : create an animation in fullscreen mode or not. Two state: 0/false -> windowed mode, 1/true -> fullscreen
: Out:
;
Procedure Anim(fullscreen.b)
  ;Init subsystem and the windows with fullscreen or not
  InitSprite()
  InitKeyboard()
  If fullscreen
    If OpenScreen(#WINWIDTH, #WINHEIGHT, #BPP, #WINTITLE) = 0
      ErrorWindowCreation()
    EndIf
  Else
    If OpenWindow(1337,0,0,#WINWIDTH, #WINHEIGHT, #WINTITLE, #PB_Window_BorderLess | #PB_Window_ScreenCentered)
      If Not OpenWindowedScreen(WindowID(1337), 0, 0, #WINWIDTH, #WINHEIGHT, 1, 0, 0)
        ErrorWindowCreation()
      EndIf
    Else
       ErrorWindowCreation()
    EndIf
  EndIf

  ;Animation
  Result = CatchSprite(0, ?fontsReps, #PB_Sprite_Texture)
  Result = CatchSprite(1, ?logoReps, #PB_Sprite_Texture)
  Result = CreateImage(12, #WINWIDTH, #WINHEIGHT,#BPP)
  ClearScreen(0)
  Repeat
  xpad = 0
  ypad = 0
  StartDrawing(ImageOutput(12))
  For i = 1 To Len(s$) Step 1
    car$ = Mid(s$, i , 1)
    Select car$
      Case Chr(10)
        ypad = ypad + #SPRH
      Case Chr(13)
        xpad = 0
      Default
        getFonts(0,xpad, ypad+156, #SPRW, #SPRH, getLetterPos(car$), 0, 0, 0)
        xpad = xpad + #SPRW
    EndSelect
  Next i
  PrintTransparentSprite(1, 0, 0, 0, 0, 0)
  StopDrawing()
   
   
 ; ClearScreen(0)
  StartDrawing(ScreenOutput())
  DrawImage(ImageID(12),0,100,640,480) ; On my pb linux version this code print at x = 0, y = 0, image height = 100 but !!! On the doc, it's drawimage(Id, x, y, w, h) so x = 0, y = 100 and image height= 480 and width = 640....
  StopDrawing() ; It's really obvious !!!! :'(
      FlipBuffers()
    If fullscreen = 0
      Event = WaitWindowEvent(0)
    EndIf
    ExamineKeyboard()
  Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Space) Or KeyboardPushed(#PB_Key_T)
 
EndProcedure

; Init the question for fullscreen or windowed mode. After selection the button, the animation will be in a good screen mode
; In :
; - text$ : Is the message for MessageBox
; Out :
;
Procedure PrintTextWindows(text$)
  Result = MessageRequester(#WINTITLE, text$,#PB_MessageRequester_YesNoCancel)
  Select Result
    Case #PB_MessageRequester_Yes
      Anim(#True)
    Case #PB_MessageRequester_No
      Anim(#False)
    EndSelect
EndProcedure



;!!!!! Begin !!!!!
  PrintTextWindows("Credits : Jace, Padman, StormBringer !!! and you ;o)"+ Chr(10) + Chr(13) + "Do you want Fullscreen ? :)")

  End ; va!n advice
;!!!!! End !!!!!

DataSection
  fontsReps: IncludeBinary("eswatfonts.bmp")
  logoReps:  IncludeBinary("eswatlogo.bmp")
EndDataSection

; IDE Options = PureBasic 4.51 (Linux - x86)
; CursorPosition = 196
; FirstLine = 158
; Folding = -
; EnableXP



The interesting code snippet :
Code: [Select]
...
; ClearScreen(0)
  StartDrawing(ScreenOutput())
  DrawImage(ImageID(12),0,100,640,480) ; On my pb linux version this code print at x = 0, y = 0, image height = 100 but !!! On the doc, it's drawimage(Id, x, y, w, h) so x = 0, y = 100 and image height= 480 and width = 640....
  StopDrawing() ; It's really obvious !!!! :'(
      FlipBuffers()
    If fullscreen = 0
      Event = WaitWindowEvent(0)
    EndIf
    ExamineKeyboard()
  Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Space) Or KeyboardPushed(#PB_Key_T)
...

Do you have the same bug with pb for windows ?

With a screenshot to proof it.
Title: Re: Requiring helps for purebasic.
Post by: padman on August 03, 2011
Quote
I'm back, so this code in "theory" will be work but not....

Well if you put the  PrintTransparentSprite(1, 0, 0, 0, 0, 0) after the Clearscreen(0) you'll see at least the logo:

Code: [Select]


(...)
 StartDrawing(ImageOutput(12))
  For i = 1 To Len(s$) Step 1
    car$ = Mid(s$, i , 1)
    Select car$
      Case Chr(10)
        ypad = ypad + #SPRH
      Case Chr(13)
        xpad = 0
      Default
        getFonts(0,xpad, ypad+156, #SPRW, #SPRH, getLetterPos(car$), 0, 0, 0)
        xpad = xpad + #SPRW
    EndSelect
  Next i

  StopDrawing()
     
  ClearScreen(0)
  PrintTransparentSprite(1, 0, 0, 0, 0, 0)                      <------- put it here
  StartDrawing(ScreenOutput())
(...)

For the rest (as far as I can tell after a quick look) you are mixing Sprites and Images up once again. The getfonts proc needs to be changed to use Images and these Images you can then plot on a Sprite. Jace's examples both work fine, why don't you just adapt one of it? ;)

The other problem really seems to be a Linux bug, looks ok on Windows.
Title: Re: Requiring helps for purebasic.
Post by: jace_stknights on August 04, 2011
I have tested your example, and it don't work for me!

And just because there are things you cannot do in purebasic, even if it's written in the help:

1- you can't sprite over a sprite !
2- you can't image over an image! (it displays at the screen)

So if you take a closer look at my examples, I'm doing this:

- image over a sprite!!! (I create an array to put the Ids)
or
- all at screen a start of frame and then grabbing all in a sprite!

And don't try to think it's nuts, just because at the end you will do it like that! Why? Just because:
1- sprites are fasters than image
2- you can create sprite3d from sprite (with #pb_sprite_texture) wich are even faster than sprite themselves! (use of the material acceleration)

I know it's a bit disturbing at start, but if you just do it like that, you will stop blasting your head!

So just use a sprite at first for the buffer, and then display image on it like I do in my example  ;D