Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Zotoaster

Member Since 10 Jul 2006
Offline Last Active Jan 13 2014 07:13 PM

Topics I've Started

Reading (and comparing) pixels in SDL2

11 January 2014 - 07:24 PM

Okay so let me explain roughly what I'm trying to do. I've got a spritesheet of a character, and I wish to attach a sword to his hand. To achieve this I'm putting two pixels on his hand per frame, one being RGB (1, 2, 3) and the other being (3, 2, 1). If I can read in and store the positions of these pixels, I should be able to position the sword as the player animates.

 

So, I've got a Texture class (which keeps a reference to the SDL surface), and I'm using this code to read pixels: http://sdl.beuc.net/sdl.wiki/Pixel_Access

 

Now, I'm going through each frame, and for each frame, going through each pixel, and comparing it to 'spec1' and 'spec2', which are Uint32 values which define the pixel colour. Here's the code:

        // read special pixels
        if (_readSpecial)
        {
            SDL_LockSurface(_texture->GetSDLSurface());
            
            // convert special SDL_Color to Uint32
            Uint32 spec1 = SDL_MapRGB(_texture->GetSDLSurface()->format, _spec1.r, _spec1.g, _spec1.b);
            Uint32 spec2 = SDL_MapRGB(_texture->GetSDLSurface()->format, _spec2.r, _spec2.g, _spec2.b);
            
            
            // start looping through frames
            for (int f = start; f < end; f++)
            {
                int frmx = f % _cols;
                int frmy = f / _cols;
                
                // get rectangle around frame
                SDL_Rect clip;
                clip.x = frmx * _frmWidth;
                clip.y = frmy * _frmHeight;
                clip.w = _frmWidth;
                clip.h = _frmHeight;
                
                bool found1 = false;
                bool found2 = false;
                
                
                // loop through all pixels in frame
                for (int y = clip.y; y < clip.y + clip.h; y++)
                {
                    if (found1 && found2) break;
                    
                    for (int x = clip.x; x < clip.x + clip.w; x++)
                    {
                        
                        // get pixel at (x, y)
                        Uint32 pix = _texture->GetPixel(x, y);

                        // if pixel is a special value, store it in animation
                        if (pix == spec1)
                        {
                            SDL_Point pt = {x, y};
                            anim->Special1.push_back(pt);
                            found1 = true;
                        }
                        else if (pix == spec2)
                        {
                            SDL_Point pt = {x, y};
                            anim->Special2.push_back(pt);
                            found2 = true;
                        }
                    }
                }
                
                // if not found special value, add dummy value
                SDL_Point none = {-1, -1};
                if (!found1) anim->Special1.push_back(none);
                if (!found2) anim->Special2.push_back(none);
            } 
            
            SDL_UnlockSurface(_texture->GetSDLSurface());
            
        }

Now, I'm putting breakpoints in the "if (pix == spec1)" statements, to check if the pixels have been found, but that breakpoint never gets reached. I'm trying to figure out why this is. Does anyone have any clues?

 

Thanks

 

 

[Edit]

 

Forgot to mention, I'm using a PNG image, not sure if that's part of the problem...


Better sword combat in my 2D platformer

10 January 2014 - 11:31 AM

Evenin',

 

I'm working on an action adventure platformer in 2D and I've just about finished up the first level, and I'm fairly satisfied, except for the sword combat. On the first quest the only thing you have to fight are simple crabs that move back and forward over their territory, and if they touch you you take some damage. This is okay because it's part of the design. But I definitely need to work on the player's combat.

 

Here's the problem. I'm using a spritesheet for the player, and all attacks are animated in that spritesheet. Each frame is only 64x64 pixels, so there's not an awful lot of room for sword attacks. For this reason I've had to keep the sword pretty short and the only animation I managed to fit was a simple stab.

 

The game doesn't have much immersion when it comes to combat. The stab doesn't appear like it should do much damage. The reaction to a stab is the crabs take a couple of steps back and a sound bite plays. But I'm just not sucked into it enough.

 

What are some key design points that I should focus on here? What makes for interesting and immersive sword fights? I'm new to the psychology of games so I'd like to hear your opinion.

 

I think it's too late to change my engine to use bone-based animation at this point, and if the character was any bigger, he'd be too big, so these are my restraints at the moment.

 

Thanks for reading


Better slope collision on platformer game

05 January 2014 - 09:43 AM

Okay so I have collision with slopes working fairly well on my platformer but I'm thinking it could be better.

 

Basically, I'm using the Tiled Map Editor, and I draw polylines on the floors where you're supposed to collide. In the game, I basically create a ray between the player's current position, and the position he's going to be in next, and check if it intersects one of these lines. If it does, the "onGround" bool is set to true (which disables gravity), and then I get the gradient of the line segment, find out how far along the X axis I am of it, multiply that by the gradient and set that as the Y coordinate. Simple maths, but it works and it's solid.

 

However, when I'm running downhill, I'm moving along the X axis, leaving the ground for a bit, falling until I hit the ground, etc. So very little time is actually spent on the ground, which means you can't jump ('onGround' has to be true to jump).

 

So, I was thinking of following these paths directly, but it's a little bit complicated and I'm confused about how to do it. Note, there are many polylines per level, and most have more than one line segment to them. Does anyone have any experience with this? I could use some help.

 

Thanks


Apparently my SDL drawing code is inefficient

31 December 2013 - 12:13 PM

Okay so I've never used SDL before and I'm just learning it now. I looked up some code on the internet and created a texture class that I use to draw. Here it is:

    /** Texture */
    Texture::Texture(string file)
    {
        SDL_Texture * newTexture = NULL;
        SDL_Surface * loadedSurface = IMG_Load(file.c_str());
        
        if (!loadedSurface)
            printf("Unable to load image %s! SDL_image Error: %s\n", file.c_str(), IMG_GetError());
        else
        {
            newTexture = SDL_CreateTextureFromSurface(SDLDevice::Instance()->GetRenderer(), loadedSurface);
            if (!newTexture)
                printf("Unable to create texture from %s! SDL Error: %s\n", file.c_str(), SDL_GetError());
            else
            {
                _width = loadedSurface->w;
                _height = loadedSurface->h;
            }
            
            
            SDL_FreeSurface(loadedSurface);
        }

        _texture = newTexture;
    }
    Texture::~Texture()
    {
        if (_texture)
            SDL_DestroyTexture(_texture);
    }
    
    int Texture::GetWidth()
    {
        return _width;
    }
    int Texture::GetHeight()
    {
        return _height;
    }
    
    void Texture::SetColour(Uint8 red, Uint8 green, Uint8 blue)
    {
        SDL_SetTextureColorMod(_texture, red, green, blue);
    }
    void Texture::SetBlendMode(SDL_BlendMode blend)
    {
        SDL_SetTextureBlendMode(_texture, blend);
    }
    void Texture::SetAlpha(Uint8 alpha)
    {
        SDL_SetTextureAlphaMod(_texture, alpha);
    }
    
    void Texture::Render(int x, int y, SDL_Rect * clip, double angle, SDL_Point * center, SDL_RendererFlip flip)
    {
        SDL_Rect renderQuad = {x, y, _width, _height};
        
        if (clip)
        {
            renderQuad.w = clip->w;
            renderQuad.h = clip->h;
        }

        SDL_RenderCopyEx(SDLDevice::Instance()->GetRenderer(), _texture, clip, &renderQuad, angle, center, flip);
    }

Apparently the 'SDL_RenderCopyEx' call is pretty inefficient, but seeing as I don't yet know how all of this really fits together, I ask if there is a better way to do this while keeping the same general interface, as it's what I'm using for my game.

 

Thanks


Relative file directoties in Xcode?

31 December 2013 - 08:08 AM

Okay so I'm using SDL and a .tmx parser to try and render some tilemaps from Tiled. I'm using Xcode with C++. So far so good.

 

But! The tilemaps' XML files refer to relative directories to find the tileset image. This is a problem because the compiled program is then looking relative to the build path rather than relative to the path of the tilemap.

 

TL;DR: Does anyone know how to access relative directories from the actual source code directories rather than the build directory?

 

Thanks

 

 

Edit: Of course "Directoties" should be "Directories". You get the picture


PARTNERS