Introduction to 2D Linux Game Programming/Algorithms/Simple Software Transformations/The Helper Functions
The Helper Functions
editRather than writing a function to print the 2D array of integers for every one of these simple examples, or manually ask for and validate input, these helper functions do the work for us. It also keeps the code cleaner, keeping the printing away from the algorithm we're showing.
The Source:
edit// S.cpp
// Static Functions
#include "include/S.h"
#include <iostream>
using namespace std;
void S::print_rect(int width, int height, int *image)
{
for(int x = 0; x < width*height; x++)
{
cout << image[x];
if((x+1) % width != 0)
cout << ",";
else
cout << "\n";
}
}
int S::sw()
{
int width;
cout << "\nEnter a value between 1 and 10 for the width: ";
cin >> width;
if(width > 10 || width < 1)
{
cout << "\nOut of Range!\n\n";
return 0;
}
return width;
}
int S::sh(int width)
{
int height;
// keeps the image from having 2 characters
if(width > 6)
{
cout << "\nWidth > 6, height set to 1";
height = 1;
}
else if(width == 5 || width == 4)
{
cout << "\nEnter a height between 1 and 2 for height: ";
cin >> height;
if(height < 1 || height > 2)
{
cout << "\nOut of Range!\n";
return 0;
}
}
else if(width == 3)
{
cout << "\nEnter a height between 1 and 3 for height: ";
cin >> height;
if(height > 3 || height < 1)
{
cout << "\nOut of Range!\n";
return 0;
}
}
else if(width == 2)
{
cout << "\nEnter a height between 1 and 5 for height: ";
cin >> height;
if(height > 5 || height < 1)
{
cout << "\nOut of Range!\n";
return 0;
}
}
else
{
cout << "\nEnter a height between 1 and 10 for height: ";
cin >> height;
if(height > 10 || height < 1)
{
cout << "\nOut of Range!\n";
return 0;
}
}
return height;
}
int S::ss(int width)
{
int scale;
switch(width)
{
case 10 :
case 9 :
cout << "\nEnter a value to scale the image between 1 and 4: ";
cin >> scale;
if (scale < 1 || scale > 4)
{
cout << "\nOut of Range!\n";
return 0;
}
break;
case 8 :
case 7 :
cout << "\nEnter a value to scale the image between 1 and 5: ";
cin >> scale;
if (scale < 1 || scale > 5)
{
cout << "\nOut of Range!\n";
return 0;
}
break;
case 6 :
cout << "\nEnter a value to scale the image between 1 and 6: ";
cin >> scale;
if (scale < 1 || scale > 6)
{
cout << "\nOut of Range!\n";
return 0;
}
break;
case 5 :
cout << "\nEnter a value to scale the image between 1 and 8: ";
cin >> scale;
if (scale < 1 || scale > 8)
{
cout << "\nOut of Range!\n";
return 0;
}
break;
case 4 :
cout << "\nEnter a value to scale the image between 1 and 10: ";
cin >> scale;
if (scale < 1 || scale > 10)
{
cout << "\nOut of Range!\n";
return 0;
}
break;
case 3 :
cout << "\nEnter a value to scale the image between 1 and 13: ";
cin >> scale;
if (scale < 1 || scale > 13)
{
cout << "\nOut of Range!\n";
return 0;
}
break;
case 2 :
cout << "\nEnter a value to scale the image between 1 and 20: ";
cin >> scale;
if (scale < 1 || scale > 20)
{
cout << "\nOut of Range!\n";
return 0;
}
break;
case 1 :
cout << "\nEnter a value to scale the image between 1 and 80: ";
cin >> scale;
if (scale < 1 || scale > 80)
{
cout << "\nOut of Range!\n";
return 0;
}
break;
default : break;
}
return scale;
}