• 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
thewhiteaussie

Q: Fast and simple way to store/manipulate pixels?

7 posts in this topic

Hello.

I'm currently experimenting with writing simple 3D engines and so far [url="http://www.youtube.com/watch?v=aC7I9fFUkF0"]isn't going too bad[/url]. I'm coding with C++ and using SDL. So far I've been using SDL's functions to generate surfaces and to manipulate pixels. However I would like to write my own class to represent surfaces, like so:

[source lang="cpp"]struct Color
{
Uint8 red;
Uint8 green;
Uint8 blue;
Uint8 alpha;
};

//Create a 1024*768 surface
Color* Screen = new Color[1024*768];[/source]

I want to achieve easy and fast color manipulation, while removing much of the dependency of a specific rendering API. Using the above method I would simply need to convert 'Screen' to the Uint32 format SDL needs at the end of the game loop.

I guess I'd like to hear some thoughts on this idea before I proceed. I'm concerned this might not end up being a very efficient way of doing things, however at present it should be faster than converting a Uint32 pixel format to RGB data and then back again (what I'm doing now); I'm only doing one conversion during runtime. Also, is there a common, generic way to represent images that isn't API specific?

Frank
0

Share this post


Link to post
Share on other sites
With your suggestion, you don't even need a "conversion" step, assuming that the API that you're using uses RGBA byte ordering.
e.g.[code]Color* Screen = new Color[1024*768];
Uint32* converted = static_cast<Uint32*>(Screen);//no conversion, just telling fibs about the data type, because they're bitwise equivalents anyway.[/code]If the API uses a different byte-ordering, you can just rearrange the order of the members in your struct, e.g.[code]struct Color
{
#if defined(API_ARGB)
Uint8 alpha;
Uint8 red;
Uint8 green;
Uint8 blue;
#elif defined(API_BRGA)
Uint8 blue;
Uint8 green;
Uint8 red;
Uint8 alpha;
#else
#error "todo - more options"
#endif
};[/code][quote name='thewhiteaussie' timestamp='1344492210' post='4967638']Also, is there a common, generic way to represent images that isn't API specific?[/quote]Yep, an array of bytes, like you're suggesting ;) Edited by Hodgman
1

Share this post


Link to post
Share on other sites
[quote name='thewhiteaussie' timestamp='1344492210' post='4967638']
I want to achieve easy and fast color manipulation, while removing much of the dependency of a specific rendering API.
[/quote]
Just a hint before you dig into your framework and experience a rude awakening later on:
If you want fast color manipulation you should not do it with the CPU. Performance wise there are lightyears between the processing power of CPU vs GPU when it comes down to pixel processing (you are literally comparing 1-8 CPU cores with [i]hundreds [/i]of GPU cores !).
0

Share this post


Link to post
Share on other sites
Hodgman: Thanks I think I get it. I'm unfamiliar with static_cast but it looks like you can read a section of memory as a different data type. I'm going to check it out.

Ashaman73: I realise that however 1) I'm still fairly inexperienced, and 2) for the environments I'm rendering, the cpu should be enough I think.
0

Share this post


Link to post
Share on other sites
[quote name='thewhiteaussie' timestamp='1344496267' post='4967653']
Hodgman: Thanks I think I get it. I'm unfamiliar with static_cast but it looks like you can read a section of memory as a different data type. I'm going to check it out.
[/quote]

Memory is just blocks of bytes, nothing more. You can read it however you want, but to prevent people from shooting themselves in the foot somewhat, C based languages are type-strict - you have to be explicit when you want to treat memory as different types.

In C++ there are 4 types of casts. static_cast, const_cast, dynamic_cast & reinterpret_cast.

static_cast is the same as doing a cast in C, like (Uint32*)Screen; It will take the pointer and give the pointer to the memory of a new type, assuming that type is 'castable' from the previous type.

For times where the types aren't castable (for example, casting Cheese* to Monkey*) you can call reinterpret_cast, which is like casting C see with a void* layer in between

dynamic_cast is the akin to the "as" cast in C#

const_cast will cast a const pointer to a non const pointer.
1

Share this post


Link to post
Share on other sites
Actually Digitalfragment just made me realise that I meant reinterpret_cast in my post earlier!

[font=courier new,courier,monospace]static_cast[/font] is only valid when the types are allowed to be converted to each other. C++ doesn't know that your RGBA struct is equivalent to an Int32, so [font=courier new,courier,monospace]static_cast[/font] will give you an error.
[font=courier new,courier,monospace]reinterpret_cast[/font] is saying "[i]I know this conversion looks dodgy, but trust me[/i]", and lets you convert any type of pointer into any other type of pointer.
0

Share this post


Link to post
Share on other sites
Working with UCHAR data in the [0, 255] range isn't the most intuitive way of working with color values. It might be more practical (though more memory consuming) to interface with normalized [0.0f, 1.0f] floating point data. Not to mention applying series of operations (blending etc.) on low-range data can cause severe artifacting. Edited by eppo
0

Share this post


Link to post
Share on other sites
Ok thanks, I think [CODE] reinterpret_cast [/CODE] will prove useful. Also I think you're righ eppo. Working with UCHAR data seems to have a few limitations.
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