Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [First SFML Project] 2D Minecraft  (Read 16648 times)

0 Members and 2 Guests are viewing this topic.

MarcuzPwnz

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
[First SFML Project] 2D Minecraft
« on: February 09, 2013, 07:45:12 am »
Here it is, my first real SFML project. I worked on this for many hours (mostly time wasted as I spent hours trying to figure out how to do stuff and failed miserably at it) It's Minecraft in 2D! :)

Features:
> 6624 Block Map with camera that scrolls across map
> Player movement including jumping and falling
> Collision detection between the player and all the blocks on the map
> Breaking & Placing blocks
> A block selector (only top 2 rows work)
> Music & sounds
> Grass grows on dirt blocks with air above them and grass blocks that don't have an air block above them turn to dirt

I created this in Xcode (on my Mac) and it compiles fine but when I tried compiling it on Windows using Code::Blocks it threw a whole bunch of errors in my face (generic C++ errors, nothing to do with SFML)



The .app file and source code can be downloaded here: http://www.mediafire.com/?c8xb0wj51tvv9mx

Most importantly, I would love it if people could criticize how badly I coded this. I am new to SFML and C++ and would really love to improve on my programming skills, all the feedback I get helps.

Thank you for reading. :)

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #1 on: February 09, 2013, 08:45:43 am »
The zip file was missing ResourcePath.hpp and ResourcePath.cpp so I ganked one from the internet ;].

I've attached my code blocks windows build -- I don't know how portable it will be (didn't use relative paths), but may be useful for reference/seeing which options I set to get it to build. 

folder structure was kept the same
Quote
<DIR>          .
 <DIR>          ..
          6,148 .DS_Store
 <DIR>          bin
         12,579 Marcuzcraft 2D v0.1.3.zip
 <DIR>          Marcuzcraft 2D.app
          1,033 MC2d.cbp
         12,468 MC2d.depend
 <DIR>          obj
            123 Read Me.txt
 <DIR>          Source

I moved the exe file once it was built to  Marcuzcraft 2D.app\Contents and copied libsndfile-1.dll and openal32.dll

Game runs now and I'll give it a whirl :)


Btw, is this pass through intentional?




Some gameplay suggestions:

*Make the white square box follow your mouse instead of arrow keys
*If player is on far left of screen and opens inventory make the inventory display on right side of screen as to not overlap character
*Movement seems rigid, try to make it a little bit more fluid (and the guy goes really fast when holding down right after a little)
*Jumps need to be a bit more fluid
*Highlight which block you select in inventory


Anyways, great work for your first project! 



Just thought of something -- did you set the option for C++11 on code blocks?

[attachment deleted by admin]
« Last Edit: February 09, 2013, 09:02:25 am by io »

MarcuzPwnz

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #2 on: February 09, 2013, 10:36:08 am »
Didn't quite understand all of that sorry :P How exactly did you get it to compile for windows? was I unable to because I was missing ResourcePath.cpp and .hpp? or did you just copy and paste it all into Code::Blocks and compile it as it was?

The passthrough is not intentional but I did know about it :)

Thanks for the awesome suggestions I will continue work on this adding the ideas you gave me! :D

What do you think of the code? how is it for a first project? was there any bad habits or better ways I could have done things?

« Last Edit: February 09, 2013, 10:46:40 am by MarcuzPwnz »

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #3 on: February 09, 2013, 11:00:58 am »
I'd be a bit of a bad resource for code advice as I just started with SFML as well :).

As for how I got it to compile, hopefully these a little clearer (with the proper setup and including the ResourcePath I got no errors on building :) ):

1) Download zip file
2) Extract to folder Marcuzcraft 2D v0.1.3
3) Create empty code blocks project in root
4) Import Source/*
5) Import Contents/Resources/* (probably not necessary)
5.a) I created the ResourcePath.cpp & hpp
6) Global Build options, check mark follow C++11 ISO standard (I'm on codeblocks 12)
7) #defines ==> SFML_STATIC
8) linker settings: sfml-audio-s,sfml-graphics-s,sfml-window-s,sfml-system-s
9) search directory (compiler to sfml 2.0 include, linker to sfml 2.0 lib)
10) compile
11) move exe to Marcuzcraft 2D.app\Contents
12) copy  libsndfile-1.dll and openal32.dll to where exe is now located


I haven't looked @ the code much, but one thing I did spot is you have no handling of the fps -- so I think if it were to run on a faster pc/slower pc your animation speeds would be different.

One thing that was suggested to me was to use one big image containing all your tiles then split them with setTextureRect

Take a peek @ http://en.sfml-dev.org/forums/index.php?topic=10363.0 , exploiters suggestions were incredibly helpful for me and you could probably apply some of them as well =0)
« Last Edit: February 09, 2013, 11:16:51 am by io »

MarcuzPwnz

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #4 on: February 09, 2013, 11:15:04 am »
Thanks again! those steps really helped. Just a few more questions  ;D

Why do you move the exe to Marcuzcraft 2D.app\Contents, on Mac the .app file is what launches the application, I just sounds odd to me.

Also where are you getting libsndfile-1.dll and openal32.dll from? Do they come with SFML? and what do they do exactly?


io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #5 on: February 09, 2013, 11:19:25 am »
Well, I used the nightly build from http://en.sfml-dev.org/forums/index.php?topic=9513.0

They were in the bin directory -- they are the dll's for openal sound system I believe.

I had to move the exe because I didnt have my paths set up correctly to search for resources in the contents folder so when code blocks tried to run the exe from the bin folder i makes it did not know about the resource folder.

MarcuzPwnz

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #6 on: February 09, 2013, 11:22:56 am »
oh awesome, it all makes sense now! :)

Thanks again for all the help!

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #7 on: February 09, 2013, 11:24:57 am »
No worries, keep up the great work!!  And don't take anything I say as a 100% accurate as I am new to this too ;].  Make note of the link I added to 2 posts before (modified the post).  I think it will be helpful for you :}}

MarcuzPwnz

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #8 on: February 09, 2013, 10:34:15 pm »
I followed your list of instructions to compile on Code::Blocks and I get the following error:

cc1plus.exe: error: unrecognized command line option '-std=c++11'

Do I need to update MinGW so it supports C++11 or something?

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #9 on: February 09, 2013, 11:32:45 pm »
Mmm, I'm on Code::Blocks 12.11 which has MingW 4.7.1 ; what version of MingW are you on?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [First SFML Project] 2D Minecraft
« Reply #10 on: February 09, 2013, 11:48:10 pm »
Also seems to build quite nicely with MinGW 4.7.2. :)

As for the code... could need quite a bit of work to get a nice and tide codebase. ;)
For instance mouseInSquare is actually already implemented with sf::Rect<T>.
sf::IntRect(x, y, width, height).contains(sf::Mouse::getPosition(window));
Or instead of setting a texture for each tile, you should use a sprite sheet and just set different texture rects. Then there's the big Main file. I'd use an Application class with function like run(), update() and draw(). Your moveSelector function could probably also get trimmed down with less 'magic'/random number places. You probably should implement a parser and keep the data (which texture part relates to with tile) in an external file, which can then get parsed on runtime. This not only gives you way more freedom in adding/removing new stuff, but it also allows you to reduce the codebase dramatically.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MarcuzPwnz

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #11 on: February 10, 2013, 12:42:03 am »
I just updated my Code::Blocks to the latest version with MinGW installer.

What's the command to check the version of MinGW I have installed?  I'm not usually on Windows. :s

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #12 on: February 10, 2013, 12:44:30 am »
Open command prompt and cd to the bin directory of mingw

Code: [Select]
C:\Program Files (x86)\CodeBlocks\MinGW\bin>"g++.exe" --version
g++.exe (tdm-1) 4.7.1
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

be sure to get the nightly build of sfml since 4.7.1 does not play nicely with build available for dl on the main site ;]

MarcuzPwnz

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #13 on: February 10, 2013, 12:51:44 am »
MinGW 4.7.1 :)

I will download the nightly build and try that out later today, I'm going out for a bit. :)

Thanks again for all the help!


Also "You probably should implement a parser and keep the data (which texture part relates to with tile) in an external file, which can then get parsed on runtime. This not only gives you way more freedom in adding/removing new stuff, but it also allows you to reduce the codebase dramatically."

I'm really new to all this and I have no clue what any of that really means, sorry. :S
« Last Edit: February 10, 2013, 12:53:18 am by MarcuzPwnz »

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: [First SFML Project] 2D Minecraft
« Reply #14 on: February 10, 2013, 12:58:55 am »
Kool, hopefully 4.7.1 will get you rolling on your windows build :D

The parser suggestion is pretty much store tile information in a "settings" file that gets read in and then used to set up various stuff.

For example,

Code: [Select]
Player.inf
Texture=Textures\wee.png
Frame1=10s
Frame2=20s
Frame3=30s

the program would read in inf and parse (make sense of) to be able to set up a player and his animations
« Last Edit: February 10, 2013, 01:01:31 am by io »

 

anything