Jump to content
  • Advertisement
Sign in to follow this  
mrtie_dye

reading and writing files(SDL)

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Please take a look at the code below. It's purpose is to load an image into an SDL_Surface, get the pixel information, and write it to a file. It is then supposed to read that file, get the pixel info, and put it on the screen. The bitmap I am using is 800X600. I know it seems kinda stupid in this context, but it is not going to be used exaclty this way. :) In the first loop in the init() function, if I replace tempColor with colors, it works as I suspect it should. So, the problem has to be in the way I am writing or reading the files. Any help is greatly appreciated. EDIT: Edited out a line pointed out by sirGustav. Still didn't fix the problem though.
#include <stdlib.h>
#include "SDL/SDL.h"
#include <fstream>

using namespace std;

//draw a pixel at cordinate x,y with color c
#define PUTPIXEL(x,y,c) ((unsigned int*)screen->pixels)[(x) + (y) * (screen->pitch / 4)] = (c);


SDL_Surface *screen;   //video surface
SDL_Surface *bg;       //background image surface

unsigned int colors[800][600];  //an array to hold the background pixel info
unsigned int tempColor[800][600]; //a temporary array to hold the background pixel info

unsigned int GetPixel(int x, int y) //get pixel info at x,y coordinate
{
    return ((unsigned int*)bg->pixels)[x + y * (screen->pitch / 4)];
}

void init()
{
    //create a temporary surface to hold the background image
    SDL_Surface *temp = SDL_LoadBMP("bg1.bmp");
    //format the background image and dump it into bg
    bg = SDL_ConvertSurface(temp, screen->format, SDL_SWSURFACE);
    //free the temporary surface
    SDL_FreeSurface(temp);
    
    //loop through the entire background's cordinates
    for (int i = 0; i < 600; i++)
    {
        for (int j = 0; j < 800; j++)
        {
            //fill the temporary array with pixel info
            tempColor[j] = GetPixel(j,i);
        }
    }
    
    //create a file stream
    fstream bFile; 

    //open the file for writing 
    bFile.open("map.dat",ios::binary|ios::out); 

    if(bFile.bad()) printf("Error opening bFile"); 
    
    //loop through the entire background's coordinates
    for(int c = 0; c < 600; c++)
    {
        for(int d = 0; d < 800; d++)
        {
            //write the temporary array to the file
            bFile.write((char*)&tempColor[d][c], sizeof(unsigned int));
        }
    }
    //close the file
    bFile.close(); 
    
    //create another file stream
    fstream iFile; 

    //open this file for reading
    iFile.open("map.dat",ios::binary|ios::in); 

    if(bFile.bad()) printf("Error opening bFile"); 
    
    //loop through the entire background's coordinates
    for(int a = 0; a < 600; a++)
    {
        for(int b = 0; b < 800; b++)
        {
            //read in the pixel information into the colors array
            bFile.read((char*)&colors[a], sizeof(unsigned int));
        }
    }
    iFile.close(); 
}

void render()
{   
  //if the screen needs to be locked, lock it
  if (SDL_MUSTLOCK(screen)) 
    if (SDL_LockSurface(screen) < 0) 
      return;

  
  //loop through the entire backgrounds coordinates
  for (int i = 0; i < 600; i++)
  {
    for (int j = 0; j < 800; j++)
    {
      //draw the background pixels using the colors array
      PUTPIXEL(j,i,colors[j]);
    }
  }

  //if the screen needed to be locked, unlock it
  if (SDL_MUSTLOCK(screen)) 
    SDL_UnlockSurface(screen);

  //update the screen
  SDL_UpdateRect(screen, 0, 0, 800, 600);    
}


int main(int argc, char *argv[])
{
  //initilize the video
  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) 
  {
    fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  
  atexit(SDL_Quit);
    
  //set the screen resolution, bpp, etc.
  screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE|SDL_FULLSCREEN);
  
  if ( screen == NULL ) 
  {
    fprintf(stderr, "Unable to set 800x600 video: %s\n", SDL_GetError());
    exit(1);
  }
  
  //call the init function
  init();

  while (1)
  {
    //call render
    render();

    //wait for the escape key to be pressed
    SDL_Event event;
    while (SDL_PollEvent(&event)) 
    {
      switch (event.type) 
      {
      case SDL_KEYDOWN:
        break;
      case SDL_KEYUP:
        if (event.key.keysym.sym == SDLK_ESCAPE)
          return 0;
        break;
      case SDL_QUIT:
        return(0);
      }
    }
  }
  return 0;
}






[Edited by - mrtie_dye on August 6, 2005 8:56:03 PM]

Share this post


Link to post
Share on other sites
Advertisement
I would cuess it has to do something with this line:
bFile << "\n";

You write a newline but doesn't read it again...

Share this post


Link to post
Share on other sites
Quote:
I would cuess it has to do something with this line:

bFile << "\n";

Doh! Too bad that still does not fix the problem. Thanks for pointing that out to me. Can't be helping much to have that in there. Any other ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by Android_s
Exactly what is your problem, and what is it you're trying to do?

Quote:
Original post by mrtie_dye
It's purpose is to load an image into an SDL_Surface, get the pixel information, and write it to a file. It is then supposed to read that file, get the pixel info, and put it on the screen.

Quote:
Original post by mrtie_dye
In the first loop in the init() function, if I replace tempColor with colors, it works as I suspect it should. So, the problem has to be in the way I am writing or reading the files

The problem is that all I get for my efforts is a black screen. Any suggestions?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!