• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
rocklobster

SDL reading 24bit texture pixels

5 posts in this topic

Hi guys,

I've recently been trying to generate a texture using a procedural method however i've ran into a problem. What i'm currently doing is looping through each pixel in a heightmap, and then summing up the pixels of x many textures by a certain weight (eg. If the heightmap pixel was fully white, then it's the highest point so a snow texture will be weighted strongly). The problem is at a certain point in this algorithm my getColourAt(int x, int y) breaks.

[CODE]
RGBColour TextureReader::getColourAt(int x, int y)
{
Uint8* p = (Uint8*)m_surface->pixels + y * m_surface->pitch + x * m_surface->format->BytesPerPixel;
Uint32 pixels = p[(y * m_surface->w) + x]; // format, &red, &green, &blue);
RGBColour rgb(red, green, blue);
return rgb;
}
[/CODE]

It breaks with x index of 109 and y index of 236. The textures i'm using are 256x256 24bit TGA textures. The only problem i've been able to narrow down to is that it might be something to do with 24bit texture and using an Uint32.

Just to clarify i've tested

[CODE]
Uint8* p = (Uint8*)m_surface->pixels + y * m_surface->pitch + x * m_surface->format->BytesPerPixel;
[/CODE]

Using Uint32* instead of Uint8* and it breaks at a different index. I've checked that the surface is valid and it is. I can view the bbp, format, width and height etc.

Thanks for any assistance.

- rocklobster
1

Share this post


Link to post
Share on other sites
First, since this is SDL, are you sure that the surface is an RGB24 surface, and not an RGBX32? Very carefully check the bitmasks for the channels, to ensure that's not the problem.

Second, what is this:
Uint32 pixels = p[(y * m_surface->w) + x]; // format, &red, &green, &blue);

Uint32 pixels = (Uint8)p[((int)y * (int)m_surface->w) + (int)x];

You are assigning a Uint8 to a Uint32. Where are red, green, and blue coming from? I get the feeling that this is not your real code.
0

Share this post


Link to post
Share on other sites
I've reworked my code from yesterday and i'm still getting some problems. I can get the image to draw, but the colors are usually just greyscale.
[CODE]
// my get colour at function
RGBColour SDLSurfaceReader::getColourAt(int x, int y)
{
Uint32 pixel;
Uint8 red, green, blue;
RGBColour rgb;
pixel = getPixel(m_surface, x, y); //getPixel function is from http://www.libsdl.org/cgi/docwiki.cgi/Pixel_Access

SDL_LockSurface(m_surface);
SDL_GetRGB(pixel, m_surface->format, &red, &green, &blue);
SDL_UnlockSurface(m_surface);
rgb.r = red;
rgb.b = blue;
rgb.g = green;
return rgb;
}

// main part of the code, recreating the surface. I'm trying to recreate it because i will eventually be blending multiple textures based on height and i just // wanted to make sure i was getting color values correctly first.

unsigned char* newTexture = new unsigned char[reader->m_surface->w * reader->m_surface->h * reader->m_surface->format->BytesPerPixel];

for (int y = 0; y < reader->m_surface->h; y++)
{
for (int x = 0; x < 3 * reader->m_surface->w; x += 3)
{
int index = (y * reader->m_surface->w) + x;
RGBColour colour = reader->getColourAt(x, y);
newTexture[index] = colour.r;
newTexture[index + 1] = colour.g;
newTexture[index + 2] = colour.b;
}
}

unsigned int id = m_renderer->createTextureFromData(newTexture, reader->m_surface->w, reader->m_surface->h, RGB);
[/CODE]
0

Share this post


Link to post
Share on other sites
Answer is here:
[url="http://stackoverflow.com/questions/10243009/convert-uint8-to-unsigned-char-rgb-values/10250809#10250809"]http://stackoverflow.com/questions/10243009/convert-uint8-to-unsigned-char-rgb-values/10250809#10250809[/url]
0

Share this post


Link to post
Share on other sites
SDL's website includes a [url="http://www.libsdl.org/docs/html/guidevideo.html"]getpixel() implementation[/url]. This will work on an arbitrary surface. Another option is to create a SDL surface with the required layout, and let SDL do the heavy lifting via SDL_BlitSurface() (or perhaps even use [url="http://sdl.beuc.net/sdl.wiki/SDL_ConvertSurface"]SDL_ConvertSurface[/url]() to combine these steps).

Now, if you are using OpenGL, you might like to steal* the implementation that is used in the [url="http://hg.libsdl.org/SDL/file/c787fb1b5699/test/testgl.c#l171"]SDL test suite[/url].

* [size="2"]It isn't actually stealing. The SDL test code is in the [url="http://hg.libsdl.org/SDL/file/c787fb1b5699/test/COPYING"]public domain[/url].[/size]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0