SFML community forums

General => SFML projects => Topic started by: tanmanknex on March 30, 2012, 05:24:22 am

Title: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on March 30, 2012, 05:24:22 am
Hello there!  As many of you know, Minecraft is a three dimensional game where you can craft and build a world of your own.  I enjoy this game, but think that one extra dimension is a bit too confusing for me ;).  I was going to rewrite it in C++ using the Allegro libraries, but I could never get them set up correctly across my multiple platforms, and it started having issues on the platform it originally worked on.  One of my friends showed me SFML, and it worked out of the box and was cross platform so I'm sticking with it.  Basically, with this version of Minecraft, I'm following the original changelog (found here: http://www.minecraftwiki.net/wiki/Version_history (http://www.minecraftwiki.net/wiki/Version_history)) as closely as I can.  So far, I have a small world working where you can place and remove blocks of different types.  You can also move a player image (not fully working) around by using the left and right arrow keys to move and the space bar to jump.  I'll update this later tonight with an OS X app and a Linux executable.  I may or may not upload the source, depending on what you guys want. 

So, in summary, this is working:
Five different block types (dirt, grass, stone, cobblestone, and wood).
Changing selected blocks (no indicator) using numbers 1-5.
Placing and removing selected block.
Loading a world from a text file.
Moving a player image and jumping (Kind of.  It's my current focus).

And the next version will have this:
Indicator for selected block type
Better player movement

Thanks for reading!  If you have any questions, feel free to email or pm me.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on March 30, 2012, 05:34:30 am
Here is the OS X .app:
https://legacy.sfmluploads.org/file/114
And the Linux executable:
https://legacy.sfmluploads.org/file/113
Title: Re: 2dCraft (Minecraft 2d port)
Post by: eXpl0it3r on March 30, 2012, 10:50:24 am
Since I'm not on linux nor Mac OSX I can't try one of your applications but I'm still left wondering how it would look like in 2D. Is it just top down or is it really just 2D?
Can't you provide a screen shot?  :)
Title: Re: 2dCraft (Minecraft 2d port)
Post by: aBallofWin on March 30, 2012, 11:49:07 am
This sounds pretty sweet! :D Sadly I'm on windows too so I can't test it, and source code would be great to look at, just to see how you did it :)
Title: Re: 2dCraft (Minecraft 2d port)
Post by: Tank on March 30, 2012, 04:14:25 pm
Redistributing Linux binaries that are linked against shared libraries is only a good idea if you provide packages for the most common distributions (.deb for Debian and derivates, RPM etc.). Otherwise, to make the process easier, link statically so the user don't has to bother with it.

I'm missing the SFML 1.6 libraries. Would be nice if you could upload it linked statically, as I don't plan to install anything prior to SFML 2.0 anyways. ;)
Title: Re: 2dCraft (Minecraft 2d port)
Post by: iHope on March 30, 2012, 04:23:38 pm
I just tested this and well, it looks very good. Of course there are bugs etc. but it looks Minecraft. Map generator is actually pretty sweet, I even played around with it. I hope you understand my english enough :D

Tip: You should put how to use map generator and controls to this thread.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on March 30, 2012, 08:40:58 pm
Thanks for the feedback so far!   Someone mentioned that I should link the libraries staticly?  How would I do that?  I'm using Code::Blocks to develop, if there's IDE specific steps. I've been programming for less than a year so I don't have a lot of experience with that.  This is the only real project I've shared online, so any suggestions would be very welcome.  I have a better version I'll upload later today that changes several things.  I'll post it along with the source code later today.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: aBallofWin on March 30, 2012, 08:42:58 pm
Thanks for the feedback so far!   Someone mentioned that I should link the libraries staticly?  How would I do that?  I'm using Code::Blocks to develop, if there's IDE specific steps. I've been programming for less than a year so I don't have a lot of experience with that.  This is the only real project I've shared online, so any suggestions would be very welcome.  I have a better version I'll upload later today that changes several things.  I'll post it along with the source code later today.

Hey, to link them statically, change in the linker options the:

-lsfml-graphics
-lsfml-window
-lsfml-system
-lsfml-audio

to

-lsfml-graphics-s
-lsfml-window-s
-lsfml-system-s
-lsfml-audio

Then that will link it statically :)
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on March 31, 2012, 12:25:27 am
So here is version 0.0.2:
OS X:
https://legacy.sfmluploads.org/file/119
Linux:
https://legacy.sfmluploads.org/file/121
Windows:
https://legacy.sfmluploads.org/file/123
Source:
https://legacy.sfmluploads.org/file/120

Changelog:
New:
To move, you now use A and D for left and right and [space] to jump like you do with the original Minecraft
You can choose which type of block to place by pushing [1-5] on your keyboard.

Fixes:
You can left click to remove a block and right click to place.  You can no longer place a block over another block.
Collisions work much better, though still aren't perfect.

If anyone would like to compile this for windows and upload it I wouldn't have any problems with that.  Any tips on how to do things better would be much appreciated!!!

Edit: There is a problem with the linux version and the source code where when the space bar is pressed it closes.  It is fixed in the OS X version and will soon be fixed in the others.  Sorry for any inconvenience!
Edit 2: Fixed in all three files!
Edit 3: Added windows binary thanks to eXpl0it3r!
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on March 31, 2012, 12:28:16 am
One more thing, I tried to link the libraries statically using what aBallofWin posted, but it says that it couldn't be found.  Any suggestions?  If not, then I suppose I can just compile a version for sfml 2.0 as well, though that'd just be one more thing to do for each post.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: eXpl0it3r on March 31, 2012, 12:44:25 pm
Okay since you now provided the source code I was able to build the stuff on my own, but only with quiet some work.
I didn't want to try SFML 1.6 since most of the applications won't run anyway on my AMD GPU so I rewrote it to use SFML 2.0 which was easy for the SFML part, but hard for the global variables. (Laurent you should disable shared context!)
Here you go with the source for SFML 2.0. It also contains a binary for Windows x64 static linked, but be aware the code is pretty much hacked together so it would work!
https://legacy.sfmluploads.org/file/123

Since the top post still misses a screenshot I created a not very spectacular one:
(https://legacy.sfmluploads.org/cache/pics/233_2dcraft.png) (https://legacy.sfmluploads.org/cache/pics/233_2dcraft.png)

Now for the codebase...
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on March 31, 2012, 04:36:51 pm
@eXpl0it3r:
Thanks for the binary and screenshot!!!  As for the code base, thanks for these suggestions!  I haven't been programming very long and I only started this project like four days ago, so the feedback on how to code is helpful. 
Clean up meaning what exactly?
How would you suggest I get rid of the global variables?  I was thinking maybe to have a global function that returns an image? 
I'll look into a resource manager a little bit in the future.
How would I use std::vector instead of an array?  Do you mean for the map layout and stuff?
What did I hard code besides using numbers for materials?
I'm going to fix the separate inputs sometime soon, that's just how it worked out because I had the world working then added entities.
Why is the mouse stuff faulty?  What happens?  I haven't noticed any problems except on my old junky computer which can barely handle a text editor and/or a compiler.
Thanks, I will!
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on April 02, 2012, 10:06:33 pm
Okay, so for the new version (probably to be released tomorrow or wednesday), I will have completely rewritten the game.  It will be version 0.0.2a and will have the following changes:
The player no longer moves around the level; the level moves around the player.
Better memory usage.
New chunk generator.
Thanks again to eXpl0it3r for the suggestions!
Title: Re: 2dCraft (Minecraft 2d port)
Post by: iHope on April 03, 2012, 01:56:02 pm
New update sounds pretty sweet! It's nice that you want continue this project :)
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on April 05, 2012, 06:59:20 pm
I apologize for not having an update yesterday but I was having issues with the random chunk generator.  That's right, the world is no longer just one file!  I have updated it so that the world moves around the player.  I'm going to be out of state for the next week (Spring Break!) but hopefully I'll have a fully working version 0.0.3 when I come back. 
0.0.3 will have:
-Random chunk generator
-Moving world
-Ability to load world from files
-Much larger walkable distance and higher ceiling for the level
-Possibly a surprise ;)
Thanks for your interest in my continuation of this project, it's why I'm still working on it!
Title: Re: 2dCraft (Minecraft 2d port)
Post by: Chuckleluck on April 13, 2012, 05:21:29 pm
Looking good!  :)
Though here's a list of a couple of bugs I've found:
Keep up the good work!  Looks very good so far.   ;D
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on April 18, 2012, 01:53:21 am
Thanks for reporting the bugs!  I'm completely redoing everything from collisions to world loading, so hopefully those bugs will be gone with the next update.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on April 20, 2012, 12:08:01 am
Hey all!  As you may have noticed, I don't have an update out.  This is due to my choice to upgrade to sfml 2.0.  I didn't think it would be too difficult, but it turns out it's more difficult than I thought.  For some reason, I'm having issues with loading textures/images and returning them from functions.  I have a couple questions.
1. Is it possible to draw an image directly to a sprite in sfml 2.0?
2. Is it possible to return a texture/image from a function (I'm thinking resource manager)?  I've tried but it returns a white texture.
3. What would the best way to draw the world to the screen?  I have it divided into three chunks with a texture for each (in my world class).
Any answer would be much appreciated!
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on April 22, 2012, 06:21:53 pm
Hey everyone, just a quick update of my progress so far. 

Here's what works:
Moving around the world (no player yet)
Loading chunks continuously (pretty much forever in each direction)
Resource Manager

Here's what doesn't work yet:
Player (I just haven't taken the time)
Collisions (no player to collide with yet)
Placing/removing blocks

After I get those last few things, I'll release an update.  You can expect it by or on Tuesday. It'll be version 0.1.0.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: eXpl0it3r on April 22, 2012, 11:18:55 pm
I'm not sure if you still need answers to those questions but I'll answer anyway.
1. Is it possible to draw an image directly to a sprite in sfml 2.0?
In SFML 2 the sf::Image got sort of replaced by sf::Texture. You now should only use sf::Image if you're doing pixel manipulation, otherwise use sf::Texture. That's mainly because the conversion from a sf::Image to a sf::Texture isn't very efficient.

2. Is it possible to return a texture/image from a function (I'm thinking resource manager)?  I've tried but it returns a white texture.
It sure is BUT if you create the texture inside that function, it will only be valid within that function and get destroyed as you leave the function. To get around this, you either have to create the texture with the new keyword but you'll have to delete the resource again on your own (just google RAII and shared_ptr to learn how to handle the basics of resource management) or you declare a member variable in which you load the texture which then will be accessable from outside this function.
For resource management I can only suggest to use the one from Thor (https://github.com/Bromeon/Thor). It will save you a lot of work and it's safe regardiong memory leaks etc (at least should be, right Nexus? ;)).

3. What would the best way to draw the world to the screen?  I have it divided into three chunks with a texture for each (in my world class).
Not exactly sure what you mean by 'world'.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on April 27, 2012, 12:59:43 am
Sorry for not having an update on Tuesday, but I was having issues with block placement and removal.  I was having a bit of trouble detecting which chunk you are clicking in, but I worked it out!  I also added a thing where the player's arm points at the mouse.  On top of that, I added some code so that the player changes direction when you push left/right!
Working:
Player movement
Block placement/removal (only dirt currently but that'll be easy to fix)
Player's arm tracking the mouse
Player direction change
Random world generation

Not working:
There is a glitch that sometimes shows up when you start out moving left where the loaded chunks jump to the right.  (Note: For some reason it's only when you start out moving left.  If you can look through my code and find why, it would be much appreciated.  If not, then it might be a while until I have a fix.)
There is a bug that displaces the chunks incorrectly if you push up/down at the start or right when the left of the screen is on a border of a chunk. (Note: This is due to the way I'm saving/loading chunks.  I'll fix it in the next update.)
Collisions (Note: This will be the next update after I release the multiple block/indicator update)

 I can release an update today, or I could wait until I have the multiple blocks and the block indicator working (probably tomorrow or next day).  What do you want me to do?
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on April 29, 2012, 08:30:41 am
Okay, everyone, here is version 0.1.0!  It is a complete rewrite of the previous version from scratch.  If you have programming experience, any advice on my coding skills would be appreciated!  I have a lot of improvements over the previous version.  Here they are:
-New world  size with random generator
-Correct block placement
-Player arm points at mouse
-Five blocks to place/remove (dirt, grass, stone, cobblestone, and wood)
-Uses wasd keys to move and 12345 to choose block type.
-Uses SFML 2.0

The next version 0.1.1 will have a new collision system, hopefully more accurate than before.  I have some ideas that I think will be really good for it, so hold tight!

Download link:
https://legacy.sfmluploads.org/file/132
((I apologize for any inconvenience, but I cannot upload a mac package until I can figure out how to install sfml 2.0 on my mac.  The source code is included should you wish to compile it yourself.  Once again, I apologize for any inconvenience.))
Title: Re: 2dCraft (Minecraft 2d port)
Post by: iHope on April 29, 2012, 08:56:38 am
If you need any help to install sfml 2.0 to mac, I'm glad to help you.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on May 06, 2012, 06:47:52 pm
@iHope: I hadn't noticed there was a .pkg installer available but I got it now.  Thanks anyway though! 

@everyone else: My next update won't contain collisions.  I tried to get them to work but it didn't work and I didn't have the patience to fix it at the moment so the next update will contain an inventory.  The one after that will have collisions.
Title: Re: 2dCraft (Minecraft 2d port)
Post by: tanmanknex on May 18, 2012, 02:43:13 am
I have great news!!!  I have (finally) gotten around to finalizing version 0.2.0!  There's some really cool features I added and some pretty major changes I've made. 

Changelog:
Added inventory support
Added the "hotbar"
Added two more block types: sand and brick
Added collisions (mostly, still a little buggy)
Added the option to switch between flying and walking
Added gravity and jumping (spacebar)
Many file system cleanups:  Source is in src folder, images are in a resources folder, and the world is in it's own folder.
I also removed all the old files that aren't being used anymore.  The whole folder should now look neater in general.

Controls:
Left click to remove block
Right click to place  selected block
1-9 to change selected block
E to open inventory
E/[esc] to close inventory
[tab] to switch between flying and walking
flying:
  W, A, S, D to move
walking:
  A, D to move
  [space] to jump

Known Bugs:
Sometimes when the player collides while walking/jumping, it freezes the game.
If you start going left first, the world glitches and jumps once.  After that it's fine.
If the world glitches (previous bug) and places you behind a block, the game freezes.  This is also why in this version you cannot place a block within a 64 pixel radius around the player.  I'm going to do something to make it handle it better in a later version.
Sometimes gravity seems immensely strong and pulls the player down instantaneously.

Download Links:
https://legacy.sfmluploads.org/file/136
This contains a codeblocks file that is completely updated, along with all the source and resources.  It also contains an xCode project file, but it hasn't been updated in quite a while, so not all of the necessary files are included and there are some that have been deleted since then.  I'll try to get around to fixing that today, but it might be a little while.

Thanks for following this project!  I'm sorry for taking so long for an update, but it's nearing the end of the school year and I've had AP tests.  Feel free to make any suggestions to me regarding code, features, bugs, or anything else!  If you have any questions, feel free to post them here and I'll do my best to answer them as soon as I can. 
Thanks again,
Tanmanknex