Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:19:34+00:00 2026-06-01T03:19:34+00:00

So I’ve set up my framework in a neat little system to wrap SDL,

  • 0

So I’ve set up my framework in a neat little system to wrap SDL, openGL and box2D all together for a 2D game.

Now how it works is that I create an object of “GameObject” class, specify a “source PNG”, and then it automatically creates an openGL texture and a box2d body of the same dimensions.

Now I am worried about if I start needing to render many different textures on screen.

Is it possible to load in all my sprite sheets at run time, and then group them all together into one texture? If so, how? And what would be a good way to implement it (so that I wouldn’t have to manually be specifying any parameters or anything).

The reason I want to do it at run time and not pre-done is so that I can easily load together all (or most) of the tiles, enemies etc.. of a certain level into this one texture, because every level won’t have the same enemies. It’d also make the whole creating art process easier.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:19:35+00:00Added an answer on June 1, 2026 at 3:19 am

    There are likely some libraries that already exist for creating texture atlases (optimal packing is a nontrivial problem) and converting old texture coordinates to the new ones.

    However, if you want to do it yourself, you probably would do something like this:

    • Load all textures from disk (your “source PNG”) and retrieve the raw pixel data buffer,
    • If necessary, convert all source textures into the same pixel format,
    • Create a new texture big enough to hold all the existing textures, along with a corresponding buffer to hold the pixel data
    • “Blit” the pixel data from the source images into the new buffer at a given offset (see below)
    • Create a texture as normal using the new buffer’s data.
    • While doing this, determine the mapping from “old” texture coordinates into the “new” texture coordinates (should be a simple matter of recording the offsets for each element of the texture atlas and doing a quick transform). It would probably also be pretty easy to do it inside a pixel shader, but some profiling would be required to see if the overhead of passing the extra parameters is worth it.

    Obviously you also want to check to make sure you are not doing something silly like loading the same texture into the atlas twice, but that’s a concern that’s outside this procedure.


    To “blit” (copy) from the source image to the target image you’d do something like this (assuming you’re copying a 128×128 texture into a 512×512 atlas texture, starting at (128, 0) on the target):

    unsigned char* source = new unsigned char[ 128 * 128 * 4 ]; // in reality, comes from your texture loader
    unsigned char* target = new unsigned char[ 512 * 512 * 4 ];
    
    int targetX = 128;
    int targetY = 0;
    
    for(int sourceY = 0; sourceY < 128; ++sourceY) {
        for(int sourceX = 0; sourceX < 128; ++sourceX) {
            int from = (sourceY * 128 * 4) + (sourceX * 4); // 4 bytes per pixel (assuming RGBA)
            int to = ((targetY + sourceY) * 512 * 4) + ((targetX + sourceX) * 4); // same format as source
    
            for(int channel = 0; channel < 4; ++channel) {
                target[to + channel] = source[from + channel];
            }
        }
    }
    

    This is a very simple brute force implementation: there are much faster, more succinct and more clever ways to copy an array, but the idea is that you are basically copying the contents of the source texture into the target texture at a given X and Y offset. In the end, you will have created a new texture which contains the old textures in it.

    If the indexing math doesn’t make sense to you, think about how a 2D array is actually indexed inside a 1D space (such as computer memory).

    Please forgive any bugs. This isn’t production code but instead something I wrote without checking if it compiles or runs.


    Since you’re using SDL, I should mention that it has a nice function that might be able to help you: SDL_BlitSurface. You can create an SDL_Surface entirely within SDL and simply use SDL_BlitSurface to copy your source surfaces into it, then convert the atlas surface into a GL texture.

    It will take care of all the math, and can also do a format conversion for you on the fly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have just tried to save a simple *.rtf file with some websites and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.