Merge branch 'SDL2'

This commit is contained in:
SGOrava 2015-04-06 21:46:46 +02:00
commit c682e5ee44
41 changed files with 566 additions and 293 deletions

2
.gitignore vendored
View File

@ -43,4 +43,6 @@ install_manifest.txt
# unsuported build
windows*
build/src
build/tetris
.kdev4*

View File

@ -1,53 +0,0 @@
[Buildset]
BuildItems=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x01\x00\x00\x00\x0c\x00T\x00e\x00t\x00r\x00i\x00s)
[CMake]
Build Directory Count=1
CMakeDir=/usr/share/cmake-3.0/Modules
Current Build Directory Index=0
ProjectRootRelative=./
[CMake][CMake Build Directory 0]
Build Directory Path=file:///home/juraj/projects/Tetris/build
Build Type=Debug
CMake Binary=file:///usr/bin/cmake
Environment Profile=
Extra Arguments=
Install Directory=file:///usr/local
[Defines And Includes][Compiler]
Name=GCC
Path=gcc
Type=GCC
[Launch]
Launch Configurations=Launch Configuration 0
[Launch][Launch Configuration 0]
Configured Launch Modes=execute
Configured Launchers=nativeAppLauncher
Name=Tetris
Type=Native Application
[Launch][Launch Configuration 0][Data]
Arguments=
Debugger Shell=
Dependencies=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x00)
Dependency Action=Nothing
Display Demangle Names=true
Display Static Members=true
EnvironmentGroup=default
Executable=
External Terminal=xterm -hold -e %exe
GDB Path=
Project Target=Tetris,tetris
Remote GDB Config Script=
Remote GDB Run Script=
Remote GDB Shell Script=
Start With=ApplicationOutput
Use External Terminal=true
Working Directory=
isExecutable=false
[SourceFileTemplates]
LastUsedTemplate=/home/juraj/.kde/share/apps/kdevfiletemplates/template_descriptions/cpp_basic.desktop

View File

@ -1,20 +0,0 @@
[Containments][1]
ActionPluginsSource=Global
activity=tetris
activityId=
desktop=-1
formfactor=0
geometry=0,0,1098,581
immutability=1
lastDesktop=-1
lastScreen=0
location=0
orientation=2
plugin=newspaper
screen=0
zvalue=0
[Project]
Manager=KDevCMakeManager
Name=tetris
VersionControl=

View File

@ -1,57 +0,0 @@
[Buildset]
BuildItems=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x00)
[CMake]
Build Directory Count=2
CMakeDir=/usr/share/cmake-2.8/Modules
Current Build Directory Index=0
ProjectRootRelative=./
[CMake][CMake Build Directory 0]
Build Directory Path=file:///home/juraj/projects/tetris/build
Build Type=
CMake Binary=file:///usr/bin/cmake
Environment Profile=default
Extra Arguments=
Install Directory=file:///usr/local
[CMake][CMake Build Directory 1]
Build Directory Path=file:///home/juraj/projects/tetris/releare
Build Type=Release
CMake Binary=file:///usr/bin/cmake
Environment Profile=Release
Extra Arguments=
Install Directory=file:///usr/local
[Launch]
Launch Configurations=Launch Configuration 0
[Launch][Launch Configuration 0]
Configured Launch Modes=execute
Configured Launchers=nativeAppLauncher
Name=Tetris
Type=Native Application
[Launch][Launch Configuration 0][Data]
Arguments=
Debugger Shell=
Dependencies=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x00)
Dependency Action=Nothing
Display Demangle Names=true
Display Static Members=true
EnvironmentGroup=default
Executable=
External Terminal=xterm -hold -e %exe
GDB Path=
Project Target=tetris,tetris
Remote GDB Config Script=
Remote GDB Run Script=
Remote GDB Shell Script=
Start With=GdbConsole
Use External Terminal=true
Working Directory=
isExecutable=false
[MakeBuilder]
Number Of Jobs=3
Su Command=2

View File

@ -1,27 +1,17 @@
cmake_minimum_required(VERSION 2.8)
set(EXE_NAME tetris)
project(tetris)
project(${EXE_NAME})
add_executable(
tetris
scoreitem.cpp
scorestate.cpp
stringinput.cpp
part.cpp
main.cpp
global.cpp
map.cpp
resource.cpp
game.cpp
pausestate.cpp
playstate.cpp
menustate.cpp
menuitem.cpp
menu.cpp
)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_INCLUDE_DIR} ${SDL2TTF_INCLUDE_DIRS})
set(TETRIS_LIBRARIES ${SDL2_LIBRARY} ${SDL2TTF_LIBRARY} )
add_subdirectory(src)
target_link_libraries(tetris SDL SDL_ttf)
set(CMAKE_CXX_FLAGS "-Wextra -ftabstop=4 -march=native -std=gnu++11 -fshow-column -ftabstop=4 -frounding-math -pipe")
install(TARGETS tetris RUNTIME DESTINATION bin)

View File

@ -1,2 +1,11 @@
<<<<<<< HEAD
# Tetris
Basic tetris write in C++ and SDL
## Changelog
* 4/6/2015
* Replaced SDL with SDL2
=======
# Tetris
My tetris game
>>>>>>> master

171
cmake/FindSDL2.cmake Normal file
View File

@ -0,0 +1,171 @@
# Locate SDL2 library
# This module defines
# SDL2_LIBRARY, the name of the library to link against
# SDL2_FOUND, if false, do not try to link to SDL2
# SDL2_INCLUDE_DIR, where to find SDL.h
#
# This module responds to the the flag:
# SDL2_BUILDING_LIBRARY
# If this is defined, then no SDL2main will be linked in because
# only applications need main().
# Otherwise, it is assumed you are building an application and this
# module will attempt to locate and set the the proper link flags
# as part of the returned SDL2_LIBRARY variable.
#
# Don't forget to include SDLmain.h and SDLmain.m your project for the
# OS X framework based version. (Other versions link to -lSDL2main which
# this module will try to find on your behalf.) Also for OS X, this
# module will automatically add the -framework Cocoa on your behalf.
#
#
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
# as appropriate. These values are used to generate the final SDL2_LIBRARY
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
#
#
# $SDL2DIR is an environment variable that would
# correspond to the ./configure --prefix=$SDL2DIR
# used in building SDL2.
# l.e.galup 9-20-02
#
# Modified by Eric Wing.
# Added code to assist with automated building by using environmental variables
# and providing a more controlled/consistent search behavior.
# Added new modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
# Also corrected the header search path to follow "proper" SDL guidelines.
# Added a search for SDL2main which is needed by some platforms.
# Added a search for threads which is needed by some platforms.
# Added needed compile switches for MinGW.
#
# On OSX, this will prefer the Framework version (if found) over others.
# People will have to manually change the cache values of
# SDL2_LIBRARY to override this selection or set the CMake environment
# CMAKE_INCLUDE_PATH to modify the search paths.
#
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
# This needed to change because "proper" SDL convention
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
# reasons because not all systems place things in SDL2/ (see FreeBSD).
#=============================================================================
# Copyright 2003-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(SDL2_ARCH_64 TRUE)
set(SDL2_PROCESSOR_ARCH "x64")
else()
set(SDL2_ARCH_64 FALSE)
set(SDL2_PROCESSOR_ARCH "x86")
endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
SET(SDL2_SEARCH_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
)
FIND_PATH(SDL2_INCLUDE_DIR SDL.h
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES include/SDL2 include SDL2
PATHS ${SDL2_SEARCH_PATHS}
)
FIND_LIBRARY(SDL2_LIBRARY_TEMP
NAMES SDL2
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES lib64 lib lib/${SDL2_PROCESSOR_ARCH}
PATHS ${SDL2_SEARCH_PATHS}
)
IF(NOT SDL2_BUILDING_LIBRARY)
IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
# Non-OS X framework versions expect you to also dynamically link to
# SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
# seem to provide SDL2main for compatibility even though they don't
# necessarily need it.
FIND_LIBRARY(SDL2MAIN_LIBRARY
NAMES SDL2main
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES lib64 lib lib/${SDL2_PROCESSOR_ARCH}
PATHS ${SDL2_SEARCH_PATHS}
)
ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
ENDIF(NOT SDL2_BUILDING_LIBRARY)
# SDL2 may require threads on your system.
# The Apple build may not need an explicit flag because one of the
# frameworks may already provide it.
# But for non-OSX systems, I will use the CMake Threads package.
IF(NOT APPLE)
FIND_PACKAGE(Threads)
ENDIF(NOT APPLE)
# MinGW needs an additional library, mwindows
# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows
# (Actually on second look, I think it only needs one of the m* libraries.)
IF(MINGW)
SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
ENDIF(MINGW)
IF(SDL2_LIBRARY_TEMP)
# For SDL2main
IF(NOT SDL2_BUILDING_LIBRARY)
IF(SDL2MAIN_LIBRARY)
SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
ENDIF(SDL2MAIN_LIBRARY)
ENDIF(NOT SDL2_BUILDING_LIBRARY)
# For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
# CMake doesn't display the -framework Cocoa string in the UI even
# though it actually is there if I modify a pre-used variable.
# I think it has something to do with the CACHE STRING.
# So I use a temporary variable until the end so I can set the
# "real" variable in one-shot.
IF(APPLE)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
ENDIF(APPLE)
# For threads, as mentioned Apple doesn't need this.
# In fact, there seems to be a problem if I used the Threads package
# and try using this line, so I'm just skipping it entirely for OS X.
IF(NOT APPLE)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
ENDIF(NOT APPLE)
# For MinGW library
IF(MINGW)
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
ENDIF(MINGW)
# Set the final string here so the GUI reflects the final state.
SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
ENDIF(SDL2_LIBRARY_TEMP)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)

163
cmake/FindSDL2_ttf.cmake Normal file
View File

@ -0,0 +1,163 @@
# Locate SDL2 library
# This module defines
# SDL2_LIBRARY, the name of the library to link against
# SDL2_FOUND, if false, do not try to link to SDL2
# SDL2_INCLUDE_DIR, where to find SDL.h
#
# This module responds to the the flag:
# SDL2_BUILDING_LIBRARY
# If this is defined, then no SDL2main will be linked in because
# only applications need main().
# Otherwise, it is assumed you are building an application and this
# module will attempt to locate and set the the proper link flags
# as part of the returned SDL2_LIBRARY variable.
#
# Don't forget to include SDLmain.h and SDLmain.m your project for the
# OS X framework based version. (Other versions link to -lSDL2main which
# this module will try to find on your behalf.) Also for OS X, this
# module will automatically add the -framework Cocoa on your behalf.
#
#
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
# as appropriate. These values are used to generate the final SDL2_LIBRARY
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
#
#
# $SDL2DIR is an environment variable that would
# correspond to the ./configure --prefix=$SDL2DIR
# used in building SDL2.
# l.e.galup 9-20-02
#
# Modified by Eric Wing.
# Added code to assist with automated building by using environmental variables
# and providing a more controlled/consistent search behavior.
# Added new modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
# Also corrected the header search path to follow "proper" SDL guidelines.
# Added a search for SDL2main which is needed by some platforms.
# Added a search for threads which is needed by some platforms.
# Added needed compile switches for MinGW.
#
# On OSX, this will prefer the Framework version (if found) over others.
# People will have to manually change the cache values of
# SDL2_LIBRARY to override this selection or set the CMake environment
# CMAKE_INCLUDE_PATH to modify the search paths.
#
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
# This needed to change because "proper" SDL convention
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
# reasons because not all systems place things in SDL2/ (see FreeBSD).
#=============================================================================
# Copyright 2003-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
SET(SDL2TTF_SEARCH_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
)
FIND_PATH(SDL2TTF_INCLUDE_DIR SDL_ttf.h
HINTS
$ENV{SDL2TTFDIR}
PATH_SUFFIXES include/SDL2 include
PATHS ${SDL2TTF_SEARCH_PATHS}
)
FIND_LIBRARY(SDL2TTF_LIBRARY_TEMP
NAMES SDL2_ttf
HINTS
$ENV{SDL2TTFDIR}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2TTF_SEARCH_PATHS}
)
IF(NOT SDL2TTF_BUILDING_LIBRARY)
IF(NOT ${SDL2TTF_INCLUDE_DIR} MATCHES ".framework")
# Non-OS X framework versions expect you to also dynamically link to
# SDL2TTFmain. This is mainly for Windows and OS X. Other (Unix) platforms
# seem to provide SDL2TTFmain for compatibility even though they don't
# necessarily need it.
FIND_LIBRARY(SDL2TTFMAIN_LIBRARY
NAMES SDL2_ttf
HINTS
$ENV{SDL2TTFDIR}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2TTF_SEARCH_PATHS}
)
ENDIF(NOT ${SDL2TTF_INCLUDE_DIR} MATCHES ".framework")
ENDIF(NOT SDL2TTF_BUILDING_LIBRARY)
# SDL2TTF may require threads on your system.
# The Apple build may not need an explicit flag because one of the
# frameworks may already provide it.
# But for non-OSX systems, I will use the CMake Threads package.
IF(NOT APPLE)
FIND_PACKAGE(Threads)
ENDIF(NOT APPLE)
# MinGW needs an additional library, mwindows
# It's total link flags should look like -lmingw32 -lSDL2TTFmain -lSDL2TTF -lmwindows
# (Actually on second look, I think it only needs one of the m* libraries.)
IF(MINGW)
SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
ENDIF(MINGW)
IF(SDL2TTF_LIBRARY_TEMP)
# For SDL2TTFmain
IF(NOT SDL2TTF_BUILDING_LIBRARY)
IF(SDL2TTFMAIN_LIBRARY)
SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTFMAIN_LIBRARY} ${SDL2TTF_LIBRARY_TEMP})
ENDIF(SDL2TTFMAIN_LIBRARY)
ENDIF(NOT SDL2TTF_BUILDING_LIBRARY)
# For OS X, SDL2TTF uses Cocoa as a backend so it must link to Cocoa.
# CMake doesn't display the -framework Cocoa string in the UI even
# though it actually is there if I modify a pre-used variable.
# I think it has something to do with the CACHE STRING.
# So I use a temporary variable until the end so I can set the
# "real" variable in one-shot.
IF(APPLE)
SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTF_LIBRARY_TEMP} "-framework Cocoa")
ENDIF(APPLE)
# For threads, as mentioned Apple doesn't need this.
# In fact, there seems to be a problem if I used the Threads package
# and try using this line, so I'm just skipping it entirely for OS X.
IF(NOT APPLE)
SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTF_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
ENDIF(NOT APPLE)
# For MinGW library
IF(MINGW)
SET(SDL2TTF_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2TTF_LIBRARY_TEMP})
ENDIF(MINGW)
# Set the final string here so the GUI reflects the final state.
SET(SDL2TTF_LIBRARY ${SDL2TTF_LIBRARY_TEMP} CACHE STRING "Where the SDL2TTF Library can be found")
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
SET(SDL2TTF_LIBRARY_TEMP "${SDL2TTF_LIBRARY_TEMP}" CACHE INTERNAL "")
ENDIF(SDL2TTF_LIBRARY_TEMP)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_ttf REQUIRED_VARS SDL2TTF_LIBRARY SDL2TTF_INCLUDE_DIR)

25
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 2.8)
add_executable(
${EXE_NAME}
scoreitem.cpp
scorestate.cpp
stringinput.cpp
part.cpp
main.cpp
global.cpp
map.cpp
resource.cpp
game.cpp
pausestate.cpp
playstate.cpp
menustate.cpp
menuitem.cpp
menu.cpp
)
target_link_libraries(${EXE_NAME} ${TETRIS_LIBRARIES})
set(CMAKE_CXX_FLAGS "-Wextra -ftabstop=4 -march=native -std=gnu++11 -fshow-column -ftabstop=4 -frounding-math -pipe")
install(TARGETS ${EXE_NAME} RUNTIME DESTINATION bin)

View File

@ -21,21 +21,22 @@ bool game::Init()
return false;
}
SDL_EnableUNICODE ( SDL_ENABLE );
gWindow = SDL_CreateWindow( "[SG]Tetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * SIZE + 130, HEIGHT * SIZE, SDL_WINDOW_SHOWN );
SDL_WM_SetCaption ( "[SG]Tetris", NULL );
m_pScreen = SDL_SetVideoMode ( WIDTH * SIZE + 130, HEIGHT * SIZE, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
if ( m_pScreen == NULL )
if ( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
cerr << "[ERROR] Cannot setup screen" << endl;
m_bRunning = false;
return false;
}
else
{
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
if ( TTF_Init() )
@ -48,9 +49,9 @@ bool game::Init()
}
m_bRunning = Load();
m_bRunning = Load();
m_bGameOver = false;
tick = 0;
tick = 0;
return m_bRunning;
}
@ -155,10 +156,14 @@ void game::Draw()
states.back()->Draw ( this );
}
SDL_Window * game::GetWindow()
{
return gWindow;
}
SDL_Surface * game::GetScreen()
{
return m_pScreen;
return gScreenSurface;
}
bool game::Running()
@ -172,9 +177,11 @@ void game::Clean()
cout << "Cleanning..." << endl;
# endif
TTF_CloseFont ( fontMenu );
TTF_Quit();
SDL_DestroyWindow( gWindow );
gWindow = NULL;
TTF_CloseFont ( fontMenu );
TTF_Quit();
SDL_Quit();
}
@ -226,3 +233,8 @@ TTF_Font * game::GetfontMenu()
{
return fontMenu;
}
void game::SetRunning ( bool Running )
{
m_bRunning = Running;
}

View File

@ -3,11 +3,12 @@
#include <iostream>
#include <vector>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "map.hpp"
#include "part.hpp"
#include "resource.hpp"
using namespace std;
@ -40,6 +41,7 @@ class game
SDL_Surface * GetScreen();
Uint32 GetStart();
SDL_Window * GetWindow();
TTF_Font * GetfontMenu();
TTF_Font * GetfontGame();
@ -56,7 +58,8 @@ class game
void UpdateFPS();
private:
SDL_Surface * m_pScreen;
SDL_Window * gWindow;
SDL_Surface * gScreenSurface;
vector <GameState *> states;
bool m_bRunning;

View File

@ -1,8 +1,8 @@
#ifndef GLOBAL_HPP_INCLUDED
#define GLOBAL_HPP_INCLUDED
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <sstream>
#include <cstdio>

View File

@ -1,7 +1,7 @@
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <SDL/SDL.h>
#include <SDL2/SDL.h>
#include "resource.hpp"
#include "global.hpp"
@ -24,7 +24,6 @@ int main ( int argc, char ** argv )
return 0;
}
Game.ChangeState ( MenuState::Instance() );
while ( Game.Running() )

View File

@ -3,8 +3,8 @@
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "global.hpp"
#include "game.hpp"

View File

@ -96,7 +96,8 @@ void MenuState::Draw(game* Game)
{
menu->Draw(Game);
SDL_Flip(Game->GetScreen());
SDL_UpdateWindowSurface( Game->GetWindow() );
//SDL_Flip(Game->GetScreen());
}

View File

@ -3,7 +3,7 @@
#include <iostream>
#include <SDL/SDL.h>
#include <SDL2/SDL.h>
#include "game.hpp"
#include "gamestate.hpp"

View File

@ -2,7 +2,7 @@
#define PART_HPP_INCLUDED
#include <iostream>
#include <SDL/SDL.h>
#include <SDL2/SDL.h>
#include "resource.hpp"
#include "global.hpp"

View File

@ -89,7 +89,8 @@ void PauseState::Draw(game* Game)
{
menu->Draw(Game);
SDL_Flip(Game->GetScreen());
//SDL_Flip(Game->GetScreen());
SDL_UpdateWindowSurface( Game->GetWindow() );
}

View File

@ -3,7 +3,7 @@
#include <iostream>
#include <SDL/SDL.h>
#include <SDL2/SDL.h>
#include "game.hpp"
#include "gamestate.hpp"

View File

@ -1,8 +1,10 @@
# include "game.hpp"
# include "states.hpp"
PlayState PlayState::m_PlayState;
void PlayState::Init()
{
Map = map();
@ -21,7 +23,7 @@ void PlayState::Init()
textColor = make_color(255, 255, 255);
SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
// SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
# ifdef DEBUG
std::cout << "PlayState Init Successful" << std::endl;
@ -30,7 +32,7 @@ void PlayState::Init()
void PlayState::Clean()
{
SDL_EnableKeyRepeat ( 0, 0 );
// SDL_EnableKeyRepeat ( 0, 0 );
# ifdef DEBUG
std::cout << "PlayState Clean Successful" << std::endl;
@ -39,7 +41,7 @@ void PlayState::Clean()
void PlayState::Pause()
{
SDL_EnableKeyRepeat ( 0, 0 );
// SDL_EnableKeyRepeat ( 0, 0 );
# ifdef DEBUG
std::cout << "PlayState Paused" << std::endl;
@ -48,7 +50,7 @@ void PlayState::Pause()
void PlayState::Resume()
{
SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
// SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
# ifdef DEBUG
std::cout << "PlayState Resumed" << std::endl;
@ -64,6 +66,7 @@ void PlayState::HandleEvents ( game* Game )
if ( event.type == SDL_QUIT)
Game->Quit();
/* Game Over - Write name for score */
if( nameEntering == true && nameEntered == false )
{
//Get user input
@ -88,13 +91,16 @@ void PlayState::HandleEvents ( game* Game )
name.show_centered(Game);
}
/* Game key logic... */
else
{
switch ( event.type )
{
/* key pressed.... */
case SDL_KEYDOWN:
switch ( event.key.keysym.sym )
{
/* ESCAPE - Pause game (run PauseState) */
case SDLK_ESCAPE:
{
//Game->SetRunning(false);
@ -102,8 +108,10 @@ void PlayState::HandleEvents ( game* Game )
}
break;
/* move part to the right */
case SDLK_RIGHT:
{
/* */
part p = Part;
# ifdef DEBUG
@ -191,7 +199,7 @@ void PlayState::HandleEvents ( game* Game )
{
Part.move ( 0, 1 );
tick = FPS - 1;
tick = ( FPS / ( ( level > 30 ) ? 30 : level ) ) - 1;
}
break;
@ -213,12 +221,16 @@ void PlayState::HandleEvents ( game* Game )
void PlayState::Update ( game* Game )
{
/* kontrola či je plné hracie pole... */
if ( Map.isFull() )
{
/* Start enter name */
nameEntering = true;
SDL_StartTextInput();
if (nameEntered)
{
SDL_StopTextInput();
nameEntering = false;
nameEntered = false;
//text.setString("Score " + intToStr(Map.getScore()) + "\n" + "Press enter");
@ -246,8 +258,12 @@ void PlayState::Update ( game* Game )
Part.move ( 0, 1 );
}
/*
if ( tick % ( FPS / ( ( level > 30 ) ? 30 : level ) ) != 0 && Map.isCollision ( Part ) && tick != ( FPS / ( ( level > 30 ) ? 30 : level ) ) -1 )
std::cout << "DEBUG" << std::endl;
*/
if ( Map.isCollision ( Part ) && tick + 1 == ( FPS / ( ( level > 30 ) ? 30 : level ) ) )
if ( Map.isCollision ( Part ) && ( tick ) == ( FPS / ( ( level > 30 ) ? 30 : level ) ) -1 )
{
int count;
@ -307,7 +323,8 @@ void PlayState::Draw ( game* Game )
name.show_centered(Game);
}
SDL_Flip ( Game->GetScreen() );
SDL_UpdateWindowSurface( Game->GetWindow() );
//SDL_Flip ( Game->GetScreen() );
}

View File

@ -1,7 +1,7 @@
# ifndef PLAY_STATE_HPP_INCLUDED
# define PLAY_STATE_HPP_INCLUDED
# include <SDL/SDL.h>
# include <SDL2/SDL.h>
# include <iostream>
# include <string>
# include <fstream>

View File

@ -2,8 +2,8 @@
#define SCOREITEM_H
# include <iostream>
# include <SDL/SDL.h>
# include <SDL/SDL_ttf.h>
# include <SDL2/SDL.h>
# include <SDL2/SDL_ttf.h>
# include "game.hpp"
# include "global.hpp"

View File

@ -16,7 +16,8 @@ void ScoreState::Draw ( game * Game )
Items[i].Draw(Game, i * 35);
}
SDL_Flip(Game->GetScreen());
//SDL_Flip(Game->GetScreen());
SDL_UpdateWindowSurface( Game->GetWindow() );
}
void ScoreState::Update ( game * Game )

4
src/states.hpp Normal file
View File

@ -0,0 +1,4 @@
#include "playstate.hpp"
#include "menustate.hpp"
#include "pausestate.hpp"
#include "scorestate.hpp"

108
src/stringinput.cpp Normal file
View File

@ -0,0 +1,108 @@
#include "stringinput.hpp"
StringInput::StringInput()
{
str = "";
text = NULL;
textColor = make_color ( 255, 255, 255 );
}
StringInput::~StringInput()
{
SDL_FreeSurface ( text );
}
void StringInput::handle_input ( game * Game, SDL_Event *event)
{
string temp;
//If a key was pressed
if ( event->type == SDL_KEYDOWN )
{
//Keep a copy of the current version of the string
temp = str;
//If the string less than maximum size
if ( str.length() <= 11 )
{
//Special key input
if( event->type == SDL_KEYDOWN )
{
//Handle backspace
if( event->key.keysym.sym == SDLK_BACKSPACE && str.length() > 0 )
{
//lop off character
str.pop_back();
}
//Handle copy
else if( event->key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL )
{
SDL_SetClipboardText( str.c_str() );
}
//Handle paste
else if( event->key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL )
{
str = SDL_GetClipboardText();
}
}
}
}
//Special text input event
else if( event->type == SDL_TEXTINPUT )
{
//Keep a copy of the current version of the string
temp = str;
//If the string less than maximum size
if ( str.length() <= 11 )
{
//Not copy or pasting
if( !( ( event->text.text[ 0 ] == 'c' || event->text.text[ 0 ] == 'C' ) && ( event->text.text[ 0 ] == 'v' || event->text.text[ 0 ] == 'V' ) && SDL_GetModState() & KMOD_CTRL ) )
{
//Append character
str += event->text.text;
}
}
}
#ifdef DEBUG
cout << str << endl;
#endif
//If the string was changed
if ( str != temp )
{
//Free the old surface
SDL_FreeSurface ( text );
//Render a new text surface
text = TTF_RenderText_Solid ( Game->GetfontGame(), str.c_str(), textColor );
}
}
void StringInput::show_centered ( game * Game )
{
//If the surface isn't blank
if ( text != NULL )
{
//Show the name
apply_surface ( 50, 85, text, Game->GetScreen() );
}
}
string StringInput::GetStr()
{
return str;
}
void StringInput::Clear()
{
str = "";
SDL_FreeSurface ( text );
text = NULL;
}

View File

@ -3,8 +3,8 @@
# include <iostream>
# include <string>
# include <SDL/SDL.h>
# include <SDL/SDL_ttf.h>
# include <SDL2/SDL.h>
# include <SDL2/SDL_ttf.h>
# include "game.hpp"
# include "global.hpp"

View File

@ -1,4 +0,0 @@
# include "playstate.hpp"
# include "menustate.hpp"
# include "pausestate.hpp"
# include "scorestate.hpp"

View File

@ -1,99 +0,0 @@
#include "stringinput.hpp"
StringInput::StringInput()
{
str = "";
text = NULL;
textColor = make_color ( 255, 255, 255 );
}
StringInput::~StringInput()
{
SDL_FreeSurface ( text );
}
void StringInput::handle_input ( game * Game, SDL_Event *event)
{
//If a key was pressed
if ( event->type == SDL_KEYDOWN )
{
//Keep a copy of the current version of the string
string temp = str;
//If the string less than maximum size
if ( str.length() <= 11 )
{
//If the key is a number
if ( ( event->key.keysym.unicode >= ( Uint16 ) '0' ) && ( event->key.keysym.unicode <= ( Uint16 ) '9' ) )
{
//Append the character
str += ( char ) event->key.keysym.unicode;
}
else if ( ( event->key.keysym.unicode == ( Uint16 ) '-' ) )
{
//Append the character
str += ( char ) event->key.keysym.unicode;
}
//If the key is a uppercase letter
else if ( ( event->key.keysym.unicode >= ( Uint16 ) 'A' ) && ( event->key.keysym.unicode <= ( Uint16 ) 'Z' ) )
{
//Append the character
str += ( char ) event->key.keysym.unicode;
}
//If the key is a lowercase letter
else if ( ( event->key.keysym.unicode >= ( Uint16 ) 'a' ) && ( event->key.keysym.unicode <= ( Uint16 ) 'z' ) )
{
//Append the character
str += ( char ) event->key.keysym.unicode;
}
}
#ifdef DEBUG
cout << str << endl;
#endif
//If backspace was pressed and the string isn't blank
if ( ( event->key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
{
//Remove a character from the end
str.erase ( str.length() - 1 );
}
//If the string was changed
if ( str != temp )
{
//Free the old surface
SDL_FreeSurface ( text );
//Render a new text surface
text = TTF_RenderText_Solid ( Game->GetfontGame(), str.c_str(), textColor );
}
}
}
void StringInput::show_centered ( game * Game )
{
//If the surface isn't blank
if ( text != NULL )
{
//Show the name
apply_surface ( 50, 85, text, Game->GetScreen() );
}
}
string StringInput::GetStr()
{
return str;
}
void StringInput::Clear()
{
str = "";
SDL_FreeSurface ( text );
text = NULL;
}