Mail Archives: djgpp/1997/12/29/14:30:23
Hello dear list, could anyone have a look at this and tell why
Map.Print(); gives me five '4' instead of just four?
--- layer.h ---
#ifndef LAYER_H
#define LAYER_H
class LayerObj {
private:
int X_SIZE;
int Y_SIZE;
int *tile_data;
public:
LayerObj(int xsize, int ysize);
~LayerObj(void);
void Assign(int x_tile, int y_tile, int newval);
void Print(void);
};
#endif
--- layer.cpp ---
#ifndef __dj_include_stdio_h_
#include <stdio.h>
#endif
#include "layer.h"
LayerObj::LayerObj(int xsize, int ysize)
{
int x,y;
/* allocate memory */
tile_data = (int *) new int[ysize][xsize];
/* init variables */
X_SIZE = xsize;
Y_SIZE = ysize;
/* clear the array */
for(y=0;y<Y_SIZE;y++)
for(x=0;x<X_SIZE;x++)
*(tile_data + (y * Y_SIZE) + x) = 0;
};
LayerObj::~LayerObj(void)
{
/* delete tile_data */
delete tile_data;
};
/* Assign:
* Assigns a value to the tile_data array.
*/
void LayerObj::Assign(int x_tile, int y_tile, int newval)
{
*(tile_data + (y_tile * Y_SIZE) + x_tile) = newval;
}
void LayerObj::Print(void)
{
/* prints the array to the screen */
int x,y;
for(y=0;y<Y_SIZE;y++)
for(x=0;x<X_SIZE;x++)
if (x == X_SIZE - 1)
printf("%d\n",*(tile_data + (y * Y_SIZE) + x));
else
printf("%d",*(tile_data + (y * Y_SIZE) + x));
};
--- test.cpp ---
#include <stdio.h>
#include "layer.h"
int main(void)
{
LayerObj Map = LayerObj(21,20);
Map.Assign(0,0,4);
Map.Assign(0,16,4);
Map.Assign(16,0,4);
Map.Assign(16,16,4);
Map.Print();
};
--- done! ---
/Jonas
- Raw text -