Jump to content

  • Log In with Google      Sign In   
  • Create Account


adt7

Member Since 03 May 2009
Offline Last Active Sep 27 2013 08:40 AM
-----

Topics I've Started

MVC: Struggling to remove graphics from the model

15 May 2013 - 07:14 AM

For my latest (small) project I thought I'd try out a slightly different way of doing things, namely I've decided to try using MVC for a game.
 
I've got a reasonable handle on it in terms of observers and events in the model and communicating these to the view (and controller), however I'm having a bit of difficultly in figuring out how to completely eliminate data about the graphical representation from the model.
 
Take this (massively simple) example of a fairly basic RPG. Since my model is data-driven, I have a single "Unit" class which contains information about the units (no need for Troll, Goblin, etc subclasses).
 
class Unit
{
    string name; //Friendly name, e.g. Dave
    int hp;
    int damage;
int range;
 
    Unit(string name, int hp, int damage);
}

Then in my model I keep a list of these Units as well as a method to retrieve them so the view can access them.
 
class Model // LevelState or similar would be a better name for this
{
    Unit[] units;
 
    Unit[] GetUnits();
}
 
The problem occurs when I come to drawing the units, as far as my model is concerned it doesn't care about if the unit is a goblin, an archer or a giant salamader, all it cares about is the unit's stats which define it's behavior.
 
However, the view very much cares about if the unit is a goblin, an archer or a giant salamader as it will have to draw different sprites for each of them.
 
What is the best way to solve this? I could add an enumerated "type" field to the Unit class, but this feels wrong as it's polluting the model with data that it doesn't care about, but I really can't see any other way of doing this.
 
Am I just overthinking and getting too concerned with what is "right" and "clean"?

Long Time Coder, First Time Going Cross-Platform

21 December 2012 - 08:48 AM

I'm not sure if this really belongs in for beginners, since it's not really a beginner question. However I feel like I'm a beginner due to my ignorance when it comes to cross-platform development.

---

A friend and I have finally finished building a fully working prototype of our 2D game (we still need to produce some more content and assets, but all the mechanics are there).

We set out to develop the game primarily for the three major mobile platforms (iOS, Android, Windows Phone 8) and the Windows Store, with Windows (non-Store), Mac and Linux as a secondary objective. Unfortunately, we didn't really think about how we were planning to achieve this in the beginning.

Ideally I want to avoid using a "write once, build anywhere" engine as I want to avoid licensing fees and this is our first time trying to go cross-platform so I'm hoping it will be a good learning experience.

Thankfully our prototype has been written so that it should be easy to seperate the code for the game mechanics and the graphical display.

I think I've identified a set of technologies that will allow us to re-use the majority of code accross all/most platforms, but I was looking for some input/advice before we set this in stone and start re-writing our prototype.

Window Creation/Input/etc.
Some platform specific "glue" (i.e. Java for Android) to create a window, handle input etc. built for each platform.

Game Mechanics
C/C++ (Compiled separately for each platform, preferably straight C if we can get away with it)

Graphics
Windows Store/Phone 8
C++ & DirectX
iOS/Android
C/C++ & SDL

My first set of questions is that we're having difficultly determining if this approach will work for the Windows Store/Phone version, specifically:

  • Can we reference external C libraries in a Windows Phone/Store app?
  • If not, will we have to re-write everything in C++/CX or are we able to use regular C/C++ for the generic parts?

The next question I think is more a symptom of my own ignorance of C++/Objective-C more than anything else:

  • As C++/Objective-C are supersets of C, if I write straight C, can I simply compile it as C++/Objective-C on the platforms where that is required? This seems like I'm asking a really stupid question here, but I just want to make sure I'm not missing something.

[XNA] and Fix Your Timestep

04 January 2011 - 08:23 AM

I'm sure all of you are familiar with the "Fix Your Timestep" article, and if you're not, then you should be.

I'm trying to achieve the same effect in an XNA game, without writing my own Game class, since the Game class does an awful lot for me that I can't be bothered to do myself.

I've come up with a fairly naive implementation, that seems to work, but I want someone with more XNA experience to take a look and see if I'm doing something that's going to cause problems.

Yes, I know I've got an allocation in my loop, but Microsoft in their wisdom decided to make the fields on the GameTime class readonly, so I'm not sure how I can get around that.


long t = 0;
const long dt = TimeSpan.TicksPerSecond / 60;

long currentTime = 0;
long accumulator = 0;

protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

long newTime = gameTime.TotalGameTime.Ticks;
long deltaTime = newTime - currentTime;

currentTime = newTime;
accumulator += deltaTime;

while (accumulator >= dt)
{
base.Update(new GameTime(gameTime.TotalGameTime, TimeSpan.FromTicks(dt)));

t += dt;
accumulator -= dt;
}

// TODO: Add your update logic here
}




Edit: In case it's not clear, I'm using the following lines to turn off VSync and allow the render loop to run as fast as possible.

graphics.SynchronizeWithVerticalRetrace = false;
IsFixedTimeStep = false;


[.net] Exposing Using Aliases

04 November 2010 - 07:02 AM

I'm currently working on an interop library for an unmanaged DLL, and to help me keep my sanity I've declared a number of using aliases for IntPtrs.

Eg.


using FooPointer = System.IntPtr;
using BarPointer = System.IntPtr;



At the moment I have methods in my library with signatures similar to the following:


public static extern FooPointer getFooPointer();



However when viewed from the customer end, the method just returns an IntPtr, my alias is not visible by the consumer.

Is there any way of exposing these aliases as part of the public interface to my library so that the different types of IntPtrs can be more easily identified?

I don't really want to have a bunch of classes and methods like the following if I can avoid it:


struct FooPointer
{
internal IntPtr Pointer;
public FooPointer(IntPtr pointer)
{
Pointer = pointer;
}
}

private static extern FooPointer getFooPointerExt();
public static FooPointer getFooPointer();
{
return new FooPointer(getFooPointerExt());
}

private static extern setFooPointerExt(IntPtr foo);
public static setFooPointer(FooPointer foo);
{
setFooPointerExt(foo.Pointer);
}



P/Invoke vs. C++/CLI (Lua library for .Net)

04 November 2010 - 03:54 AM

I've spent a long time recently trying to find a decent Lua binding for .Net and everything I've found keeps coming up short.

The "best" (and I use the word loosely) has been Tao.Lua, which is no longer being worked on, and is simply a bunch of P/Invoke calls into the unmanaged Lua library.

I've spent a bit of time tidying it up (returning bool instead of int etc.) and correcting all of the bits of functionality that didn't work as they should.

The next step was to give it a bit more of a .Net-ish interface since at the moment it passes around and returns a hell of a lot of IntPtrs, and generally isn't that great to work with on the C# side of things.

My question is though, should I continue building on the P/Invoke stuff that is in place, or start over with a C++/CLI based solution.

I'll be honest, I don't know enough about how P/Invoke or C++/CLI work - even after reading as much as I could find from Google - to make an informed choice.

My intention is to release this as a free to use library when it's complete, and people using it could potentially end up making 1000s of P/Invoke calls every update of their game loop.

Most of the P/Invoke calls have only two arguments, an IntPtr and then either a primitive type or an IntPtr. Is switching it to C++/CLI going to make a great deal of difference in terms of per-call overhead in this case?

PARTNERS