Jump to content
  • Advertisement
Sign in to follow this  
salvenger

SDL Class, looking good so far?

This topic is 3084 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

Hello, I am just wondering is this code is looking proper, organized, and is the working ok for it?

using System;

using Tao.Sdl;

namespace Map_Editor
{
class SDL
{
//Screen--------------
struct screen
{
public int width, height, bit;
public IntPtr ptr, format;
}
screen sdlScreen;
//--------------------

//Surfaces------------
struct struct_surfaces
{
public int width, height;
public IntPtr ptr, format;
}

private struct_surfaces[] surfaces = new struct_surfaces[1];
//--------------------

//Key events----------
private bool[] keys = new bool[323];
//--------------------

//Mouse events--------
public const byte MOUSE_BUTTON_LEFT = 1;
public const byte MOUSE_BUTTON_MIDDLE = 2;
public const byte MOUSE_BUTTON_RIGHT = 3;
public const byte MOUSE_BUTTON_SCROLL_UP = 4;
public const byte MOUSE_BUTTON_SCROLL_DOWN = 5;

public const byte MOUSE_POSITION_X = 0;
public const byte MOUSE_POSITION_Y = 1;

private int[] mousePos = new int[2]; //Two positions: X and Y
private bool[] mouseButton = new bool[6]; // Five buttons: Left, Middle, Right, Scroll Down, Scroll Up
//--------------------

//FPS-----------------
//private bool regulateFPS = true;
//private int maxFPS = 30;
//private int startTicks = 0, updateTicks = 0, endTicks = 0;
//--------------------

public SDL(string caption, int width, int height, int bit, bool fullscreen)
{
sdlScreen = new screen();
if (Sdl.SDL_Init(Sdl.SDL_INIT_EVERYTHING) == 0)
{
sdlScreen.width = width;
sdlScreen.height = height;
sdlScreen.bit = bit;

Console.WriteLine("Initialized SDL.");

sdlScreen.ptr = Sdl.SDL_SetVideoMode(width, height, bit, Sdl.SDL_DOUBLEBUF | Sdl.SDL_HWSURFACE);
if (sdlScreen.ptr != IntPtr.Zero)
{
if ((sdlScreen.format = createSurface(sdlScreen.ptr).format) == IntPtr.Zero) quit();
Sdl.SDL_WM_SetCaption(caption, null);
}
else
{
Console.WriteLine("Initialize failed.");
Console.ReadLine();
quit();
}

Console.WriteLine("Initialize - OK");
}
}

public void begin()
{
handleInputs();

Sdl.SDL_Rect rect = new Sdl.SDL_Rect(0, 0, (short)sdlScreen.width, (short)sdlScreen.height);
Sdl.SDL_FillRect(sdlScreen.ptr, ref rect, Sdl.SDL_MapRGB(sdlScreen.format, 0, 125, 125));
}

public void end()
{
if(!(Sdl.SDL_Flip(sdlScreen.ptr) == -1)) return;
}

public void quit()
{
//Free Surfaces----------------------------------
if (surfaces != null)
{
if (surfaces.Length > 0)
{
for (int i = 0; i < surfaces.Length; i++)
{
if (surfaces.ptr != IntPtr.Zero)
{
Sdl.SDL_FreeSurface(surfaces.ptr);
}
}
}
}
Sdl.SDL_FreeSurface(sdlScreen.ptr);
//-----------------------------------------------

Sdl.SDL_Quit();
Environment.Exit(0);
}

public void addSurface(string file, byte[] RGB)
{
if (!(System.IO.File.Exists(file))) { Console.WriteLine(file + " doesn't exist."); return; }
if (RGB.Length < 3) { Console.WriteLine("Invalid color key."); return; };
if (surfaces == null) { Console.WriteLine("'Surfaces' can not be null."); return; }

for (int i = 0; i < surfaces.Length; i++)
{
if (surfaces.ptr == IntPtr.Zero)
{
IntPtr tempPtr;

tempPtr = SdlImage.IMG_Load(file);
if (tempPtr == IntPtr.Zero) return;
surfaces.ptr = Sdl.SDL_DisplayFormat(tempPtr);
surfaces.format = createSurface(surfaces.ptr).format;
Sdl.SDL_FreeSurface(tempPtr);
if (surfaces.ptr == IntPtr.Zero) return;
int colorKey = Sdl.SDL_MapRGB(surfaces.format, RGB[0], RGB[1], RGB[2]);
Sdl.SDL_SetColorKey(surfaces.ptr, Sdl.SDL_SRCCOLORKEY, colorKey);

struct_surfaces[] tempArray = new struct_surfaces[surfaces.Length];
Array.Copy(surfaces, tempArray, surfaces.Length);
surfaces = new struct_surfaces[tempArray.Length + 1];
Array.Copy(tempArray, surfaces, tempArray.Length);
break;
}
}
}

public void drawSurface(int surface, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY)
{
if (surfaces == null) { Console.WriteLine("'Surfaces' can not be null."); return; }
if (surface > surfaces.Length) { Console.WriteLine("Invalid surface."); return; }
if (surfaces[surface].ptr == IntPtr.Zero) { Console.WriteLine("Surface #" + surface + " is not set."); return; }

Sdl.SDL_Rect srcRect = new Sdl.SDL_Rect((short)srcX, (short)srcY, (short)srcWidth, (short)srcHeight);
Sdl.SDL_Rect destRect = new Sdl.SDL_Rect((short)destX, (short)destY, 0, 0);
Sdl.SDL_BlitSurface(surfaces[surface].ptr, ref srcRect, sdlScreen.ptr, ref destRect);
}

private Sdl.SDL_Surface createSurface(IntPtr ptr)
{
return ((Sdl.SDL_Surface)System.Runtime.InteropServices.Marshal.PtrToStructure(ptr, typeof(Sdl.SDL_Surface)));
}

public bool getKeyState(int key) { return (keys[key]); }

public int[] getMousePos { get { return (mousePos); } }
public bool[] getMouseButtonState { get { return (mouseButton); } }

private void handleInputs()
{
Sdl.SDL_Event events;
while (Sdl.SDL_PollEvent(out events) == 1)
{
switch (events.type)
{
case Sdl.SDL_QUIT:
quit();
break;

case Sdl.SDL_KEYDOWN:
keys[events.key.keysym.sym] = true;
Console.WriteLine(events.key.keysym.sym);
break;

case Sdl.SDL_KEYUP:
keys[events.key.keysym.sym] = false;
break;

case Sdl.SDL_MOUSEMOTION:
mousePos[0] = events.motion.x;
mousePos[1] = events.motion.y;
break;

case Sdl.SDL_MOUSEBUTTONDOWN:
mouseButton[events.button.button] = true;
break;

case Sdl.SDL_MOUSEBUTTONUP:
mouseButton[events.button.button] = false;
break;

default:
break;
}
}
}
}
}


Using the code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Map_Editor
{
class Program
{
static void Main(string[] args)
{
SDL SDL = new SDL("Test", 640, 480, 32, false);
Map map = new Map((640 / 32), (480 / 32));

SDL.addSurface(Environment.CurrentDirectory + "\\GFX\\Tiles\\Set1.png", new byte[] {0, 0, 0});

while (true)
{
SDL.begin();

SDL.drawSurface(0, 32, 0, 32, 32, 32, 32);
for (int x = 0; x < map.width; x++)
{
for (int y = 0; y < map.height; y++)
{
SDL.drawSurface(0, 32, 0, 32, 32, x * 32, y * 32);
}
}

if (SDL.getKeyState(27)) //ESC key
{
break;
}

Console.WriteLine(SDL.getMousePos[SDL.MOUSE_POSITION_X]);
SDL.end();
}
SDL.quit();
}
}
}



Also, is it ok to contain a class within a class?
That is what I am doing for my map, like so:


using System;

namespace Map_Editor
{
class Map
{
//Map---------------------------------------------------
private struct_Map data;

struct struct_Map
{
public int width, height;
public Tile[,] tile;
}

public Map(int width, int height)
{
data = new struct_Map();
data.width = width;
data.height = height;
data.tile = new Tile[width, height];

for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
data.tile[x, y] = new Tile();
}
}
}

public int width { get { return data.width; } }
public int height { get { return data.height; } }

public int getLayer(int x, int y, int layer)
{
if (data.tile == null) { Console.WriteLine("Tile can not be null."); return -1; }
if (!(x < data.width)) { Console.WriteLine("X value must be with in range."); return -1; }
if (!(y < data.height)) { Console.WriteLine("Y value must be with in range."); return -1; }
if (data.tile[x, y].layer == null) { Console.WriteLine("Layer can not be null."); return -1; }
if (!(layer < data.tile[x, y].layer.Length)) { Console.WriteLine("Layer must be with in range."); return -1; }

return data.tile[x, y].layer[layer];
}

//Tiles-------------------------------------------------
class Tile
{
public int[] layer = new int[(int)enum_layer.count - 1];
public int[] type = new int[(int)enum_type.count - 1];

enum enum_layer
{
ground, ganim, mask, manim, fringe, fanim, count
}
enum enum_type
{
none, blocked, count
}

public Tile()
{

}
}
//------------------------------------------------------
}
}



Thanks in advance,
Salvenger

(I might be going to bed here soon so I'll check back in the morning.)

Share this post


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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!