Input / Output with SDL (using Dev- C++)

Hello everyone, I’m just starting off with SDL so I apologize before hand if
this question is too much of a newbie question.

I’m programming a cellular automata for my programming class and was thinking
about usind SDL for the graphics layout (I’d use monitor pixels as cells and
give them an evolution law according to their RGB value). The probelm is that
after adding the SDl code interface to my program’s sourcecode, I can’t use
console I/O anymore. Te be more exact, the program compiles correctly without
any errors but when I run it it just freezes on the windows command prompt
console. Could someone please help me out?? I’m attaching my source code to
this post so that you may see exactly what I’m doing. If you try runnign it,
note that I haven’t set the evolution law yet so it won’t work. Nonetheless
I’d like to know why the program won’t show the consolo I/O which are in the
source code right after “int main()”.

Thank You very very much before hand as this information is of enormeous value
to me.

//SOURCE CODE//

#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <fstream.h>

#include <SDL/SDL.h>

using namespace std;

class pixel_state {
public:
unsigned int R : 8, G : 8, B : 8;
pixel_state () {};
pixel_state operator + (pixel_state);
pixel_state operator += (pixel_state);
pixel_state operator = (pixel_state);
pixel_state operator / (double);
pixel_state operator * (double);
};

pixel_state pixel_state :: operator + (pixel_state param) {
pixel_state temp;
temp.R = R + param.R;
temp.G = G + param.G;
temp.B = B + param.B;
return (temp);
}

pixel_state pixel_state :: operator += (pixel_state param1) {
pixel_state temp1;
temp1.R = temp1.R + param1.R;
temp1.G = temp1.G + param1.G;
temp1.B = temp1.B + param1.B;
return (temp1);
}

pixel_state pixel_state :: operator = (pixel_state param4) {
pixel_state temp4;
temp4.R = param4.R;
temp4.G = param4.G;
temp4.B = param4.B;
return (temp4);
}

pixel_state pixel_state :: operator / (double param2) {
pixel_state temp2;
temp2.R = (unsigned int)(R / param2);
temp2.G = (unsigned int)(G / param2);
temp2.B = (unsigned int)(B / param2);
return (temp2);
}

pixel_state pixel_state :: operator * (double param3) {
pixel_state temp3;
temp3.R = (unsigned int)(R * param3);
temp3.G = (unsigned int)(G * param3);
temp3.B = (unsigned int)(B * param3);
return (temp3);
}

int random_seed () {

int rand_seed;
srand((unsigned int)time((time_t *)NULL));
rand_seed = rand();
return rand_seed;

}

int random_range(int lowest_number, int highest_number)
{
srand((unsigned int)time((time_t *)NULL));
if(lowest_number > highest_number){
int q = highest_number;
lowest_number = highest_number;
highest_number = q;
}

int range = highest_number - lowest_number + 1;
return lowest_number + int(range * rand()/(RAND_MAX + 1.0));

}

pixel_state** initialize_species (long k, int seed) {

pixel_state **array;

array = new pixel_state* [160];
for (int i=0; i<160; i++) array[i] =new pixel_state [120];

for (int n=0; n<160; n++) {
      for (int l; l<120; l++) {
          srand(seed+random_range(0,k));
          float R_temp = (rand()/RAND_MAX)*255;
          float G_temp = (rand()/RAND_MAX)*255;
          float B_temp = (rand()/RAND_MAX)*255;
          array[n][l].R = (int)R_temp;
          array[n][l].G = (int)G_temp;
          array[n][l].B = (int)B_temp;
          }
	}

return array;

}

pixel_state** evolve_species (pixel_state **array, int L) {

pixel_state **array_to_evolve, counter;

array_to_evolve = new pixel_state* [160];
for (int i=0; i<160; i++) array_to_evolve[i] =new pixel_state [120];

for (int n=0; n<160; n++) {
for (int l=0; l<120; l++) {

    int k;
	counter.R = 0;
    counter.G = 0;
    counter.B = 0;

 for (k=1; k<=L; k++) {

  if ((n-k)<0 && (n+k)<L) counter += (array[n-k+160][l]+array[n+k][l]);
  if ((n-k)<0 && (n+k)>L) counter += (array[n-k+160][l]+array[n+k-160][l]);
  if ((n-k)>0 && (n+k)>L) counter += (array[n-k][l]+array[n+k-160][l]);
  if ((n-k)>0 && (n+k)<L) counter += (array[n-k][l]+array[n+k][l]);

  if ((l-k)<0 && (l+k)<L) counter += (array[n][l-k+120]+array[n][l+k]);
  if ((l-k)<0 && (l+k)>L) counter += (array[n][l-k+120]+array[n][l+k-120]);
  if ((l-k)>0 && (l+k)>L) counter += (array[n][l-k]+array[n][l+k-120]);
  if ((l-k)>0 && (l+k)<L) counter += (array[n][l-k]+array[n][l+k]);
			}
        array_to_evolve[n][l] = ((counter + (array[n][l]*4)) / (4*k*k+3));
                          }
     }

return array_to_evolve;

}

pixel_state** array_COPY (pixel_state **array) {

 pixel_state **array_COPY;

 array_COPY = new pixel_state* [160];
 for (int i=0; i<160; i++) array_COPY[i] =new pixel_state [120];


 for (int n=0; n<160; n++) 
   for (int l=0; l<120; l++) 
 array_COPY[n][l] = array[n][l];
 
 return array_COPY;

}

bool stability_control(pixel_state **array, pixel_state **array_comp) {

int n, l;
for (n=0; n<160; n++) {
for (l=0; l<120; l++) {
if ((array[n][l].R != array_comp[n][l].R) ||
(array[n][l].G != array_comp[n][l].G) ||
(array[n][l].B != array_comp[n][l].B)) break;
}
}
if (n<(159) || l<(119)) return false;
else return true;
}

void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
if ( SDL_LockSurface(screen) < 0 )
{
return;
}
}
}

void Sulock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
SDL_UnlockSurface(screen);
}
}

void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel)
{
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 )screen->pixels + yscreen->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 )screen->pixels + yscreen->pitch/4 + x;
*bufp = color;
}
break;
}
}

void DrawScene(SDL_Surface *screen, pixel_state **array)
{
Slock(screen);

float x_pixel_per_cell = floor(640/159.9);
float y_pixel_per_cell = floor(480/119.9);

for(float x=0; x<640; x++)
{
for(float y=0; y<480; y++)
{
int Red = array[(int)(x/(x_pixel_per_cell + 0.7))][(int)(y/
(y_pixel_per_cell + 0.7))].R;
int Green = array[(int)(x/(x_pixel_per_cell + 0.7))][(int)(y/
(y_pixel_per_cell + 0.7))].G;
int Blue = array[(int)(x/(x_pixel_per_cell + 0.7))][(int)(y/
(y_pixel_per_cell + 0.7))].B;
DrawPixel(screen,(int)x,(int)y,Red,Green,Blue);
}
}
Sulock(screen);
SDL_Flip(screen);
}

int main (int argc, char* argv[]) {

pixel_state **evo_array, **evo_array_check;
int Population, Years, iter, strati, seed, check_counter=0;
long stati;
char buffer [20], *check_answer=new char, *check_answer_2=new char,
*no={“n”}, *yes={“y”};
float temp_par;

system(“cls”);

cout << "How do you wish to save the current evolution file? " << flush;
cin.getline(buffer, 20);

while (1) {
if ((fopen(buffer,“r”))) {
cout << “\n\nFile already exists! Do you wish to Overwrite?
(Y / N)”;
cin >> check_answer;
if (!strcasecmp(check_answer,no))
{
system(“cls”),
cout << "How do youwish to save the current
evolution file? ",
cin.getline(buffer,20); }
else {system(“cls”);
break;}}
else break;
}

system(“cls”);

cout << "\n\nHow many levels of influence do you want there to be? ";
cin >> strati;

while (1) {
if (strati > 239) {
cout << “\n\nToo much company!!”;
cout << “\n\nDo you want the program to choose the maximum
possible?(Y / N)”;
cin >> check_answer_2;
if (!strcasecmp(check_answer_2,no))
{
system(“cls”);
cout << "\n\nHow many levels of
influence do you want there to be? ";
cin >> strati; }
else
{strati = 239; break;}
}
else break;
}

cout << "\n\nHow many different states of existence do you want there to be? ";
cin >> stati;

cout << “\n\nFor how many steps do you want the automata to evolve? \n\n”;
cin >> Years;

ofstream evolution(buffer);

seed = random_seed();

evolution << seed << “\n\n” << stati << “\n\n” << strati;

evo_array = initialize_species(stati, seed);

for (iter=0; iter<=Years; iter++) {

evo_array = evolve_species(evo_array, strati);

if (stability_control(evo_array, evo_array_check) && check_counter==0) {

              check_counter++;

              cout << "\n\n\nEVOLUTION BREAK: \n\nAutomata stability has 

been reached after “
<< iter << " generations.”
<< “\n\nFurther evolution has been found unnecessary.”
<< “\n\nDo you want evolution to continue anyways? (Y/N)
\n\n\n\n\n”;
cin >> check_answer;
if (!strcasecmp(check_answer,no))
{break;}
else {cout << “\n\nUser has decided to continue evolution
anyways.\n\n\n\n\n”;}}

evo_array_check = array_COPY(evo_array);

}

cout << “\n\n\n\t\t\tAUTOMATA EVOLUTION COMPLETE!!!”;

evolution.close();
getchar();
system(“cls”);
}

I think this is caused by SDL’s redirection of stdout and stderr into
text files. Check the directory where your program ran, and look for a
file named stdout.txt - it should have the first line of your
program’s output in it.

I believe there’s a define you can change that will cause SDL not to
redirect those two streams, but I’m not sure what it is. Check the SDL
docs for more info.

Hope this helps,

-JustinOn Thu, 20 Jan 2005 16:08:25 +0000 (UTC), Daniel <zombie_1985 at hotmail.com> wrote:

The probelm is that
after adding the SDl code interface to my program’s sourcecode, I can’t use
console I/O anymore. Te be more exact, the program compiles correctly without
any errors but when I run it it just freezes on the windows command prompt
console.