Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Game engine resources?  (Read 12478 times)

0 Members and 1 Guest are viewing this topic.
Game engine resources?
« on: July 10, 2014, 03:34:42 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Been trying to code my own lightweight basic 2D game engine with SFML.net.  I've seen a few websites about 2d game engines but they usually go way beyond the scope of what I want early on.

I have only two source files thus far.  It's in C# in case you could not tell.

PROGRAM.cs
Code: [Select]
/*
 * Created by SharpDevelop.
 * User: owner
 * Date: 7/8/2014
 * Time: 3:58 AM
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;

namespace HyruleEngine
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("This is the Hyrule Engine Version 0.0.0.1");
Console.WriteLine("Features in this release: Basic engine functions.");

// TODO: Implement Functionality Here

Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);


GameCore app = new GameCore();
app.Run();



}
}
}

GameCore.cs
Code: [Select]
/*
 * Created by Michael Jeffries
 * User: owner
 * Date: 7/8/2014
 * Time: 4:27 AM
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using SFML;
using SFML.Audio;
using SFML.Graphics;
using SFML.Window;

namespace HyruleEngine
{
/// <summary>
/// Description of GameCore.
/// </summary>
public class GameCore
{
RenderWindow window;
bool gameRunning;
Music musicLoop;
Music musicStart;

public GameCore()
{


}
protected void Init()
{
//Game Initialization code goes here
window = new RenderWindow(new VideoMode(400, 224), "Hyrule Engine");//setting up our basic resolution for our game window
window.SetFramerateLimit(30);//Setting a base frame rate of 30
musicStart = new Music("Resources/Music/lttpOverworldStart.ogg");//test music
musicLoop = new Music("Resources/Music/lttpOverworldLoop.ogg");//blam
musicStart.Play();//playing the test muic
gameRunning = true;//bool for the game loop


}
public void Run()
{

Init();


//main loop
while (gameRunning == true)
{
if (Keyboard.IsKeyPressed(Keyboard.Key.Escape))
{
window.Close();
}
if ((musicStart.Status == 0) && (musicLoop.Status == 0))
{
musicLoop.Play();
musicLoop.Loop = true;
}



window.Clear(Color.Blue);

window.Display();
}
}



}
}


I hope I am on the right track.  What I am aiming for is a simple engine that runs via states: Title, File Select, Game, End/Game Over.  I want drawable objects that draw themselves.  When I worked with XNA, that's how I handled objects that were drawn/active.

I do need to work more on SFML as well.  The damn documentation for .net is skimpy at best.  Been trying to correlate the normal C++ docs to the .net docs.

Anyway, if anyone has any resources , I'd appreciate it.  I should mention that the engine runs fine as is right now so I haven't broken anything yet.
Logged
  • Super Fan Gamers!

Starforsaken101

Wake the Beast
Re: Game engine resources?
« Reply #1 on: July 10, 2014, 11:53:34 am »
  • www.mouffers.com
  • *
  • Reputation: +69/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2577
Very good start, and no you haven't broken anything yet but you can very easily with this code hehe.

Just looking at GameCore.cs you might want to learn about proper variable/method protection (public/private/protected). It's not a big deal at the moment but you have a lot of public variables and stuff. That makes it so any class outside can access it which I'm not sure you want for GameCore.cs.

Equally, seeing a game loop in a while loop is dangerous and I wouldn't honestly recommend it. If you have the chance, I would recommend you look at Update loops instead.

EDIT: Okay turns out I know jack !@#$% about .NET but regardless this loop is not efficient because of reasons.

I can go on but that's basic enough for now.

I haven't dabbled in .NET so I can't really...give you much help with that. I've been absorbed with Unity/C# as of late tight butthole.
Logged
  • Starforsaken101's DeviantART
Re: Game engine resources?
« Reply #2 on: July 11, 2014, 02:41:41 am »
  • Personal Saucier
  • *
  • Reputation: +17/-1
  • Offline Offline
  • Posts: 249
Been trying to code my own lightweight basic 2D game engine with SFML.net.  I've seen a few websites about 2d game engines but they usually go way beyond the scope of what I want early on.

I have only two source files thus far.  It's in C# in case you could not tell.

PROGRAM.cs
Code: [Select]
/*
 * Created by SharpDevelop.
 * User: owner
 * Date: 7/8/2014
 * Time: 3:58 AM
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;

namespace HyruleEngine
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("This is the Hyrule Engine Version 0.0.0.1");
Console.WriteLine("Features in this release: Basic engine functions.");

// TODO: Implement Functionality Here

Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);


GameCore app = new GameCore();
app.Run();



}
}
}

GameCore.cs
Code: [Select]
/*
 * Created by Michael Jeffries
 * User: owner
 * Date: 7/8/2014
 * Time: 4:27 AM
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using SFML;
using SFML.Audio;
using SFML.Graphics;
using SFML.Window;

namespace HyruleEngine
{
/// <summary>
/// Description of GameCore.
/// </summary>
public class GameCore
{
RenderWindow window;
bool gameRunning;
Music musicLoop;
Music musicStart;

public GameCore()
{


}
protected void Init()
{
//Game Initialization code goes here
window = new RenderWindow(new VideoMode(400, 224), "Hyrule Engine");//setting up our basic resolution for our game window
window.SetFramerateLimit(30);//Setting a base frame rate of 30
musicStart = new Music("Resources/Music/lttpOverworldStart.ogg");//test music
musicLoop = new Music("Resources/Music/lttpOverworldLoop.ogg");//blam
musicStart.Play();//playing the test muic
gameRunning = true;//bool for the game loop


}
public void Run()
{

Init();


//main loop
while (gameRunning == true)
{
if (Keyboard.IsKeyPressed(Keyboard.Key.Escape))
{
window.Close();
}
if ((musicStart.Status == 0) && (musicLoop.Status == 0))
{
musicLoop.Play();
musicLoop.Loop = true;
}



window.Clear(Color.Blue);

window.Display();
}
}



}
}


I hope I am on the right track.  What I am aiming for is a simple engine that runs via states: Title, File Select, Game, End/Game Over.  I want drawable objects that draw themselves.  When I worked with XNA, that's how I handled objects that were drawn/active.

I do need to work more on SFML as well.  The damn documentation for .net is skimpy at best.  Been trying to correlate the normal C++ docs to the .net docs.

Anyway, if anyone has any resources , I'd appreciate it.  I should mention that the engine runs fine as is right now so I haven't broken anything yet.

I miss XNA. Based on what you've started so far, it shows you have the understandings of the basic of the game loop which is great. I used to work on a project that accomplished some of these goals you have back when XNA was still officially supported by Microsoft. If you're interested in learning about states and state switching, consider using an interface for each state -- what does the game engine need to call or pass down into these states from the game loop? In XNA it was Update/Draw, and some initialization functions per-state.

You can also consider to add a frames-per-second handler that will monitor and can throttle the rate of these Update/Draw calls. XNA was 'smart enough' to know when to update more often than it did draw. I'm not honestly sure how it made that determination, however.

Anyways, cool stuff. I haven't posted here in a while, but reading about your start kind of inspired me.
Logged
Re: Game engine resources?
« Reply #3 on: July 11, 2014, 06:43:19 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Not going to lie, I am trying to emulate XNA a little.  XNA was my favorite framework to mess with other than Game Maker.  Monogame isn't bad but afaik it still requires you build an XNA Content project for resources or something along that line.

I know a bit about states as my unfinished projects used states for changing levels and menu screens.  My issue now is making my own motor to run things instead of XNA.

I plan on working on the interface idea as SFML.net provides a Drawable interface which I need to learn more about.  Just juggling a job, construction project, 3 vehicle projects, and rehabilitating my dog which recently had a stroke..my plate is quite full. 

Star, I agree about the while loop.  My eventual plan though was to have the player be able to break out of the loop with either closingthe window or pressing escape.  The while loop will call updates to a single "level"(screen) which in turn handles its own drawing and events.

Would post some more but I am posting ona phone and my fingers hurt.  xD
Logged
  • Super Fan Gamers!

Starforsaken101

Wake the Beast
Re: Game engine resources?
« Reply #4 on: July 11, 2014, 01:19:19 pm »
  • www.mouffers.com
  • *
  • Reputation: +69/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2577
Haha yeah XD I posted about the fact that the while loop was super goofy but Steve noted that this is something sort of common in .NET? I don't know man. I'm so used to Unity now that I'm sort of giving all of the other languages the same treatment I guess.
Logged
  • Starforsaken101's DeviantART
Re: Game engine resources?
« Reply #5 on: March 01, 2015, 06:48:26 am »
  • *
  • Reputation: +0/-1
  • Offline Offline
  • Posts: 22
Wow, I didn't know SFML had a .NET binding. Cool! That's worth considering instead of Monogame, which I'm currently using.

I gave up SFML with C++ for a couple reasons, almost a year ago when I started seriously doing game development. The thing that got me to quit was that I ran into this glitch that I just could not, for the life of me, find in the code. Someone told me that it might be lack of memory management and variables getting rewritten, or something. Idk. That frustrated me and made me quit. Another thing was that I couldn't find any way with SFML to see if a song had stopped playing, which I needed for syncing up the title screen properly.

As for your code: Yeah, you do seem to be really copying XNA, haha. I, personally, would take advantage of the simplicity that SFML offers. I would drop the GameCore class and the Program.cs file, and just use a simple int main() function to store all that stuff in. Not sure if this is good practice or not, but if you're cut loose from the ties of XNA, it'd be worth trying.
« Last Edit: October 06, 2015, 02:12:43 pm by HylianGoombario »
Logged
AKA HylianDev and Super Goombario
Re: Game engine resources?
« Reply #6 on: March 01, 2015, 06:25:04 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Someone told me that it might be lack of memory management and variables getting rewritten, or something. Idk. That frustrated me and made me quit.
I'm sorry, because I'm about to sound like a bit of a !@#$%. If learning how to manage memory is "frustrating" enough to make you quit, then maybe you should find something else to do because programming clearly isn't going to be a great choice.

I, personally, would take advantage of the simplicity that SFML offers. I would drop the GameCore class and the Program.cs file tight butthole, and just use a simple int main() function to store all that stuff in. Not sure if this is good practice or not, but if you're cut loose from the ties of XNA, it'd be worth trying.
No, it is not a good practice. In fact, it's a very, very bad one. Properly structuring your code is one of the most important things you can do if you want to actually be able to complete a project any more complex than "Hello, World!".

If you actually want to learn to program, whether it's games, software, websites, whatever then you really should learn to do it properly. Otherwise, just use RPG Maker or some other tool that does everything for you.
« Last Edit: March 01, 2015, 06:40:10 pm by Cassyblanca »
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Re: Game engine resources?
« Reply #7 on: March 04, 2015, 06:19:00 am »
  • *
  • Reputation: +0/-1
  • Offline Offline
  • Posts: 22
Someone told me that it might be lack of memory management and variables getting rewritten, or something. Idk. That frustrated me and made me quit.
I'm sorry, because I'm about to sound like a bit of a !@#$%. If learning how to manage memory is "frustrating" enough to make you quit, then maybe you should find something else to do because programming clearly isn't going to be a great choice.
The problem with it was, I don't even know how to do that. What could I have been doing wrong? I couldn't find any tutorials on it at the time either. Basically the way I saw it was, it's not working as I'm programming it and C# apparently doesn't require memory management, so I'd rather use that and it work.

I, personally, would take advantage of the simplicity that SFML offers. I would drop the GameCore class and the Program.cs file tight butthole, and just use a simple int main() function to store all that stuff in. Not sure if this is good practice or not, but if you're cut loose from the ties of XNA, it'd be worth trying.
No, it is not a good practice. In fact, it's a very, very bad one. Properly structuring your code is one of the most important things you can do if you want to actually be able to complete a project any more complex than "Hello, World!".

If you actually want to learn to program, whether it's games, software, websites, whatever then you really should learn to do it properly. Otherwise, just use RPG Maker or some other tool that does everything for you.
you're a bit of a fireball aren't you

I'm not talking about improperly structuring your code, I'm talking about taking the dozen or so lines of code from Update and maybe mingling them into the same function as the dozen or so lines of code from Draw, for the sake of simplicity, instead of making a Game class. If that's bad practice, then fine, but please don't talk to me like that
Logged
AKA HylianDev and Super Goombario
Re: Game engine resources?
« Reply #8 on: March 04, 2015, 06:40:03 am »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
...
The problem with it was, I don't even know how to do that. What could I have been doing wrong? I couldn't find any tutorials on it at the time either. Basically the way I saw it was, it's not working as I'm programming it and C# apparently doesn't require memory management, so I'd rather use that and it work.
Yes, C# does require memory management. While you aren't responsible for allocating, tracking and deallocating memory to the extent that you are in C, you still have to pay attention to the lifetime of your objects and be careful of how many and where you are allocating these. Garbage collector sweeps can be massive sources of performance problems in games.

I'm not talking about improperly structuring your code, I'm talking about taking the dozen or so lines of code from Update and maybe mingling them into the same function as the dozen or so lines of code from Draw, for the sake of simplicity, instead of making a Game class.
I'm sorry but yes, you are talking about improperly structuring the code. Right now, sure, the methods are only "a dozen or so lines." But believe it or not, games take more than a "dozen or so lines" to update, and more than a "dozen or so lines" to perform rendering logic. And if the advice to "stick everything in main()" were followed, eventually the code would be an absolute mess. Encapsulating game logic into its own class is a good practice - the only thing that should go into main should be logic necessary to initialize and clean up the game/application.

If that's bad practice, then fine, but please don't talk to me like that
If the advice you offer is bad, I'm going to call you on it. Because otherwise, somebody's going to see that advice and think "oh, I'll just throw everything in main! I don't need to consider the fact that my code is going to expand later and become a maintenance nightmare!"
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Re: Game engine resources?
« Reply #9 on: March 04, 2015, 06:54:12 am »
  • *
  • Reputation: +0/-1
  • Offline Offline
  • Posts: 22
I said stick it in main because this is what my Update code looks like in my engine:

Code: [Select]
        protected override void Update(GameTime gameTime)
        {

            gamedata.input.Update();

            gamedata.writer.Update(gamedata);

            gamedata.cam.Update(gamedata.input.p_up, gamedata.input.p_left);

            gamedata.screens[gamedata.current_screen].Update(gamedata);

            base.Update(gameTime);
        }

and because this is what it looks like under draw:

Code: [Select]
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Viewport = new Viewport(
                0,
                (graphics.PreferredBackBufferHeight / 2) - ((graphics.PreferredBackBufferWidth / (int)gamedata.resolution.X) * (int)gamedata.resolution.Y) / 2,
                graphics.PreferredBackBufferWidth,
                (graphics.PreferredBackBufferWidth / (int)gamedata.resolution.X) * (int)gamedata.resolution.Y
            );

            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, gamedata.cam.GetTransform() * Matrix.CreateScale(new Vector3((float)graphics.PreferredBackBufferWidth / gamedata.game_width, (float)graphics.PreferredBackBufferWidth / gamedata.game_width, 1.0f)));

            gamedata.screens[gamedata.current_screen].Draw(spriteBatch, textures, gamedata);

            gamedata.writer.Draw(spriteBatch, textures[7], new Vector2(0,0), gamedata);

            base.Draw(gameTime);
        }

and it probably won't be changing a whole lot. If it does change, it'll be a line or two. GameData contains stuff like different screens, save data, camera, etc.

Personally, I don't think it'd be a crime if these things were side-by-side. That was my point. Not that you should badly structure code, but that creating a class just so you can have three functions didn't seem necessary to me.

---

About the memory management. Just like in the issue before, we're apparently talking about different things. I'm just saying that, when I used C++, a variable in my class was apparently being overwritten or something, somehow, causing very undesirable results. At least, that's what someone told me when I talked to them about it. I was quite new to C++ at the time, so the idea that my variable could just be overwritten willy-nilly in the memory because of something that was going on in the background that I didn't know about turned me off of it. Was this what was really happening? Idk. But I switched to C# because it was recommended to me, and I prefer it.
Logged
AKA HylianDev and Super Goombario
Re: Game engine resources?
« Reply #10 on: March 04, 2015, 01:33:36 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Quote
when I used C++, a variable in my class was apparently being overwritten or something, somehow, causing very undesirable results.

Yes, C# allows you to do that too.  You probably had 2 pointers to the same variable and modified one of them, therefore modifying the original variable.  C# allows this same thing through reference types.  Or you did a buffer overflow (less likely).  Which is undefined behavior and can cause all kinds of mayhem.
Logged



i love big weenies and i cannot lie
Re: Game engine resources?
« Reply #11 on: March 04, 2015, 03:12:55 pm »
  • Personal Saucier
  • *
  • Reputation: +17/-1
  • Offline Offline
  • Posts: 249
I'm just saying that, when I used C++, a variable in my class was apparently being overwritten or something, somehow, causing very undesirable results.

To be honest, you can do the same exact thing in C# if you are not careful. Like MG-Zero said, you probably had two references pointing to the same address -- which can happen in C# too. This can be avoided by staying organized and being aware of what you're doing -- which is a concept global to computer science in general.
Logged
Re: Game engine resources?
« Reply #12 on: March 04, 2015, 05:28:55 pm »
  • *
  • Reputation: +0/-1
  • Offline Offline
  • Posts: 22
I'm totally aware that it's possible, if you're using a pointer. It's been so long, and I probably don't even have the source anymore, but the variable in question was the one that told the Draw function which sprite in the sheet to draw. It was malfunctioning after I set another variable. It was as if I had "made too many variables" or something. It was literally the declaration of a variable tht broke my engine.

I was new so I consulted someone else, who told me it was probably a memory management error, and that memory management was less manual in C#. I listened.
Logged
AKA HylianDev and Super Goombario
Re: Game engine resources?
« Reply #13 on: March 04, 2015, 06:35:53 pm »
  • Personal Saucier
  • *
  • Reputation: +17/-1
  • Offline Offline
  • Posts: 249
I'm totally aware that it's possible, if you're using a pointer.

All variables that point to an object in C# are technically pointers to that object (aka reference types). Are you saying you were using a value type (int, double, etc...) and it was still overriding the reference?

It's been so long, and I probably don't even have the source anymore, but the variable in question was the one that told the Draw function which sprite in the sheet to draw. It was malfunctioning after I set another variable. It was as if I had "made too many variables" or something. It was literally the declaration of a variable tht broke my engine.

I was new so I consulted someone else, who told me it was probably a memory management error, and that memory management was less manual in C#. I listened.
This doesn't really make any sense, unless you got the too many variables error from adding excessive parameters to a function. Otherwise, it sounds like a mistake was made in the code that probably threw you and your friend off the trail.

If you have any specific questions that you need help on with C# (or other languages even), feel free to post snippets of code here.
Logged
Re: Game engine resources?
« Reply #14 on: March 04, 2015, 06:42:33 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
The thing with C/C++ is that it lets you do whatever the hell you want, regardless of whether or not the specification regards it as undefined behavior.  When you DO do something undefined however, things get wacky.  Writing too much data to a buffer, returning the address of local memory etc.  When you do these types of things, the behavior of the program is no longer predictable and strange things such as overwriting of variables can occur.
Logged



i love big weenies and i cannot lie
Re: Game engine resources?
« Reply #15 on: March 04, 2015, 09:06:40 pm »
  • *
  • Reputation: +0/-1
  • Offline Offline
  • Posts: 22
@Lorentz I was using C++ for this project. Not to mention, I was new to real programming. Who knows what kind of goofy mistake I made. But after a while of troubleshooting I determined that it was fixed when I commented out a variable I had newly initialized, and broken when I uncommented it. It made no sense to me, and motivated me to quit.

@MG-Zero, that sounds a lot like what hapened but I have no idea how I could've done that while trying to make my first C++ platformer engine hehe. I sure didn't know anything about messing with buffers and etc
Logged
AKA HylianDev and Super Goombario
Re: Game engine resources?
« Reply #16 on: March 04, 2015, 09:10:56 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Well a buffer is just a variable.  If you try to shove a long into an int, you're gonna have overflow problems.
Logged



i love big weenies and i cannot lie
Re: Game engine resources?
« Reply #17 on: March 05, 2015, 12:47:45 am »
  • *
  • Reputation: +0/-1
  • Offline Offline
  • Posts: 22
...oh. haha. I'm getting curious so I might try to dig up my old source code when I get home
Logged
AKA HylianDev and Super Goombario
Re: Game engine resources?
« Reply #18 on: March 07, 2015, 06:29:00 am »
  • *
  • Reputation: +0/-1
  • Offline Offline
  • Posts: 22
Update: I no longer have the source code so who knows what the bug was about.
Logged
AKA HylianDev and Super Goombario
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.103 seconds with 76 queries.