Thread: Vertical Scrolling Bitmap in SDL

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Vertical Scrolling Bitmap in SDL

    im making a RAPTOR clone (i know, been done many times) in SDL, and i'm having trouble getting the image to scroll vertically for the background.
    Im using one bitmap for the background, and it scrolls, but doesnt loop.
    The code im using comes from the cone3d lesson 6 source for horizontal scrolling, edited for vert. However, when the image runs out, its gone forever lol.
    Can anyone help me get the code right so that it DOES work?

    Code:
    float scroll;
    
    void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
    {
      SDL_Rect dest;
      dest.x = x;
      dest.y = y;
      SDL_Rect dest2;
      dest2.x = x2;
      dest2.y = y2;
      dest2.w = w;
      dest2.h = h;
      SDL_BlitSurface(img, &dest2, screen, &dest);
    }
    
    void DrawScroll()
    {
      if(scroll>3909-480)
      {
        DrawIMG(back, 0, 0,(int)(480),(int)(3909-scroll),0,(int)(scroll));
        DrawIMG(back, 0,(int)(3909-scroll-1),640,(int)(480-(3909-scroll)),0,0);
      } else {
        DrawIMG(back, 0,0,640,4480,0,(int)scroll);
      }
    }
    thanks in advance!

    -Blizz
    This war, like the next war, is a war to end war.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Can I make a suggestion? This is a space game right?

    I assume that your using the bitmap for the stars, and as you move you scroll the bitmap to animate the stars?

    The way i've done it before is set up 3 arrays of random pixels, each array's pixels slightly darker(simulates distance)

    Then move each array of pixels at different velocities. (close = fast; far = slow)

    If this isn't a space game, well then....

    DW

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I do not use the SDL but there is a very easy way in Direct3D.

    You can use a translation matrix and set it accordingly. Then set your texture transform to the appropriate mode and use the translation matrix you created.

    This is how I scrolled my clouds across the sky in my terrain engine. Direct3D already has built in functionality for scrolling bitmaps.

    Here is some sample code from my CSkyPlane class which is derived from CTexturedPlane. However I think you should be able to see what is going on w/o posting the entire source for CTexturedPlane.


    Code:
    void CSkyPlane::Render(void)
    {
     
    //Setup render states
    //Will not be done here later - will be done in state blocks
    _Device->SetRenderState(D3DRS_LIGHTING, false);
    _Device->SetRenderState(D3DRS_ZWRITEENABLE,false);
    _Device->SetStreamSource(0,_VB,0,sizeof(TexturedPlaneVertex));
    _Device->SetFVF(TexturedPlaneVertex::FVF);
    _Device->SetIndices(_IB);
    _Device->SetTexture(0,_Texture);
     
    //For translucency in clouds - if clouds lower than mountains..mountains visible
    //slightly through terrain - lowers frames a lot
    //_Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
     
    D3DXMATRIX world=_rot*_pos;
    _Device->SetTransform(D3DTS_WORLD,&world);
     
    //Advanced texturing - disable for now
    //Possible later display option for altered method of rendering sky
    //Part of the translucent rendering and altered lighting rendering
    //_Device->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
    //_Device->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_TEXTURE);
    //_Device->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_ADD);
     
    //Scroll the texture
    _curtu+=_tu;
    _curtv+=_tv;
    if (_curtu>(_scale-.5f)) _curtu=0.0f;
    if (_curtv>(_scale-.5f)) _curtv=0.0f;
     
    //Setup translation matrix
    D3DXMATRIX ttrans;
    D3DXMatrixIdentity(&ttrans);
     
    //Set appropriate members of matrix for east/west scrolling
    //Can be changed to accomodate any type of 2D scrolling
    ttrans._31=_curtu;
    ttrans._32=_curtv;
     
    //Set transform to texture transform and use above matrix as input matrix
    _Device->SetTransform(D3DTS_TEXTURE0,&ttrans);
    _Device->SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT2);
     
    //Set texture to current texture and render as one large primitive - very very fast
    _Device->SetTexture(1,_Texture);
    _Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
    		 0,0,4,0,2);
     
     
    _Device->SetRenderState(D3DRS_LIGHTING, true);
    _Device->SetRenderState(D3DRS_ZWRITEENABLE,true);
    _Device->SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_DISABLE);
     
    //Only needed if we used altered method of rendering - translucent rendering
    //_Device->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
     
    }
    Notice that because this uses the transform and lighting pipeline all scrolling is done in hardware not software. This results in extremely smooth and extremely efficient scrolling.

    However it only works well for single textures. It is possible to use a tile engine with this if you render all your world/tiles to one big texture and then use this to scroll the world. However, if the quad is too large you might end up losing some resolution in your texture.

    You could build the tile world on loadup of the level and load in an alpha map the same size as the map. Set all alphas to 0 except for the area that the player is in. This will result in a fog of war type effect and will auto-smooth the edges of the viewing area if you set the alpha based on distance from the player. Quite nice. Once the map is built all you need to do is scroll it with the transform in Direct3D - no extra loading, rendering, etc. Also for the fog of war you simply have to access the alpha texture and set alpha blending to the correct states.....all done in hardware. Should give very good frames.
    Last edited by VirtualAce; 09-17-2004 at 08:23 PM.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Death_Wraith:
    have you used SDL before?
    correct me if im wrong but i think direct pixel drawing is quite slow in SDL.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    No, never used SDL. Just DirectX(which i am currently learning atm)

    And, well I lied a bit! Someone I know wrote a game using the pixel plot. It works just fine, and looks super nice as well. Here is a 32-bit pixel plot function I wrote (well André LaMothe wrote =) )

    Code:
    // this builds a 32 bit color value in A.8.8.8 format (8-bit alpha mode)
    #define _RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24))
    ///////////////////////////////////////////////////////////////////////////////
    
    
    inline void __fastcall TForm1::Plot_Pixel32(int x, int y, int alpha,
                                    int red, int green, int blue,
                                    UINT *video_buffer, int lpitch)
    {
    //this function assumes that the user has already locked the surface,
    //and cleared ddsd
    
    // first build up color WORD
    UINT pixel = _RGB32BIT(alpha,red,green,blue);
    
    //write the data
    video_buffer[x + y*lpitch] = pixel;
    }
    x,y are the coords
    alpha is the alpha in A.8.8.8 32-bit RGB mode
    red,green,blue = RGB values of each color
    videobuffer = mainDirectsurface.lsurface
    lpitch = the memory pitch of the vid card


    hope this helps

    DW

  6. #6
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Well, 2 things:

    1:
    This is NOT a space shooter, some of the elvels will be, but past the point of pixel plotting.

    2:
    I know nothing about translation matrixes, etc

    Sandman: Yes, you are correct, but it's even slower in allegro, which i have been able to counteract in my own picture format :P

    I make a virtual bitmap, then plot the pixels to that when loading the graphics, then use them as bitmaps. works fine. However, that will not do in this game as my pixel format is a scripting language for graphics, and is ussually 420 % bigger then bitmaps for a 100x100 pixel file.

    So thanks for the star field code, but thats not gonna help me out enough.

    I was really hoping for the code i posted to be reaaranged in a way it works for verticalscrolling, i was just unable to do so myself.

    Ill keep the star stuff in mind for overlayer effects though!
    This war, like the next war, is a war to end war.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Hmmm...sorry I couldn't help. have you tried GameDev?

    Try some of these links:


    SDL scrolling

    GFX w/SDL


    I hope this helps,

    Good luck,

    DW

  8. #8
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Well, i tried gamedev, and that had a bunch of people asking and getting links to the ones you gave me, which happens to be where i got the code that i posted originally LOL

    I just dont understand the code well enough, but now im thinkg that, after maing a bitmap for 1/8 level size, and that was 8 megs, that i could use smaller chunks, ill try that and keep you guys posted on progress.
    This war, like the next war, is a war to end war.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Alrighty then, sorry I couldn't really help. Good luck with that eh?

    DW

  10. #10
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    yea thanks. But maybe you can figure out the function well enough to make it work, just by finding out what goes where?
    If not, thats okay, appreciate you trying.
    This war, like the next war, is a war to end war.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Use a 2D per pixel scrolling algo. Direct writes to the buffer will not be slow. It is simple pointer addition. If you want more help I will post the code and the exe in a zip for you to look at. It should not be hard to implement in SDL.

    Per pixel scrolling turned up about 50000 hits on google. Just sort through them till you get to the sites that really discuss what you need. Google has been getting broader in it's searches, at least for me. It appears as though they are taking more liberty with your searches than before.
    Last edited by VirtualAce; 09-19-2004 at 08:10 PM.

  12. #12
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Ah, per pixel scrolling, thats a new term for me, ill look for it, thanks!
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  3. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  4. Bitmap scrolling with scroll bars
    By solar3147 in forum Windows Programming
    Replies: 0
    Last Post: 03-17-2003, 02:39 AM
  5. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 10:07 AM