wardred
Member
- Joined
- Dec 18, 2008
- Messages
- 394
- Website
- thorslongboat.com
- WEBSITE
- https://artstation.com/wardred
- YOUTUBE
- wardred22
I'm creating a very simple SDL Mario Brothers like side scroller - it'll be ready AT LEAST 2 months after the Pandora is :b . I was wondering if anybody would be interested in tutorials like the one below. I'd be writing these up as I encounter "topics" I think others would be interested in.
Edit: Cleaned up the comments just a tad - they need to be trimmed, but this is just to see if there's interest.
Edit2: There would also be an "examining the code" bit after the code to explain exactly what those nested for loops are doing.
Code:
#include <iostream>
/********************************************
*
* This is example code for simple integer
* scaling algorithm for integer values >= 1.
*
* The algorithm is handy in 2d sidescrolling
* games that were originally designed for a
* smaller screen - say a Game Boy - when
* being played on a screen with at least
* double the horizontal and vertical
* resolution of the original - say a PC's
* monitor.
*
* It keeps the original image's aspect ratio.
* If the original aspect ration was 4:3 and
* you have a wide screen monitor, there
* will be black vertical bars on either side
* of the screen using this algorithm, but the
* image will not be distorted. Princess
* Peach, for instance, will not have to worry
* about why she's suddenly so much wider. :b
*
* It makes an array that is width*height sized.
* It initializes every element in
* the array with its array position.
*
* Then it makes a second array that
* is width*height*scale sized.
*
* It prints the first array, a couple newlines,
* then it scales and prints the second array.
*
* It uses simple ints and couts to minimize
* complexity and show the underlining algorithm.
*
* Try changing width, height, and scale and
* see what happens.
*
* The array will look out of shape if it has to
* print any number 10 or higher because now
* you're printing 2 characters for each element
* instead of 1.
*
* If you keep width*height < 12, 11 technically
* though that's a prime number, the results
* should look fine. (An array starts with
* element 0, so in a 10 character array,
* the 10th element is position 9.)
*
* It will also look skewed if it has to wrap
* a line around the screen.
*
********************************************/
int main()
{
int width=3; // The original Array's width
int height=3; // The original Array's height
int scale=3;
int source[width*height]; // Original Array
// int dest[width*height*scale]; Edit: This will only give you 27 instead of 81. Bad, Bad me!
int dest[width*scale * height*scale]; // Scaled Array
for(int x = 0; x < width*height; x++)
{
source[x]=x;
std::cout << x;
if((x+1) % width != 0)
std::cout << ",";
if((x+1) % width == 0)
std::cout << std::endl;
}
std::cout << std::endl << std::endl;
// The following ints defined out of
// for loop to avoid recreating them
// every time we drop out of inner loops
int x = 0; // x = height counter
int y = 0; // y = height repeat
int z = 0; // z = width counter
int t = 0; // t = width repeat
for(; x < height; x++) // Source Height Position
{
for(; y < scale; y++) // Repeat a full row scale times
{
for(; z < width; z++) // Source width Offset
{
// Repeat individual pixel scale times
for(; t < scale; t++)
{
dest[(x*width*scale)+t] = source[(x*width)+z];
std::cout << dest[(x*width*scale)+t];
if( ((z*scale)+t+1)%(width*scale)!=0)
std::cout << ",";
if( ((z*scale)+t+1)%(width*scale)==0)
std::cout << std::endl;
}
t = 0;
}
z = 0;
}
y = 0;
}
std::cout << std::endl << std::endl;
return(0);
}
Edit: Cleaned up the comments just a tad - they need to be trimmed, but this is just to see if there's interest.
Edit2: There would also be an "examining the code" bit after the code to explain exactly what those nested for loops are doing.