35 Commits

Author SHA1 Message Date
5f018e24cf MakeFile almost working need rule to build .\build%.o 2022-10-12 17:56:54 -04:00
0c8ec81b74 Windows Branch using mingw toolchain 2022-10-12 16:27:22 -04:00
391fcedc37 libSDL2 static build and link 2022-09-30 16:09:12 -04:00
1dec4347e0 Built SDL2_image and _mixer static 2022-09-30 15:49:16 -04:00
e2605bf6c1 Changed to static libraries for libTmxParser 2022-07-18 18:09:09 -04:00
21c30b7f52 Before changing to static libraries 2022-07-04 09:42:42 -04:00
70402c5382 udpated SDL_GetTicks() to SDL_GetTicks64() 2022-05-30 14:02:21 -04:00
4aa7fe1692 predictCollisions working in debug mode 2022-05-14 17:52:05 -04:00
36adcdeac7 predictCollisions func added to Game.cpp 2022-05-13 23:20:08 -04:00
e28a9cebdb TileMap Debug Colliders Drawing 2022-05-01 17:35:58 -04:00
b94c2cff38 Debug menu scale fixed, Player Tile Position 2022-05-01 15:38:55 -04:00
fb20b8d868 Camera and Tilemaps fixed at 1x res 2022-05-01 11:00:51 -04:00
9f4007bb79 TileMaps and camera tuning 2022-04-29 21:31:34 -04:00
bc4262d552 Continued work on tilemaps/debug 2022-04-08 18:19:27 -04:00
2331110c71 TileMaps and cam debug added 2022-04-07 23:20:19 -04:00
c8aae59f26 TileMaps now render and move with camera 2022-04-04 18:16:20 -04:00
871e6d19c6 TileMaps now render each row correctly 2022-03-27 23:59:12 -04:00
72b4ac9229 TileMapComponent now takes a TMX to render tilemap 2022-03-24 23:34:17 -04:00
dc43e59ff6 halsafar/libtmx-parser added to project 2022-03-21 15:26:13 -04:00
66b8de6f40 Cleaned up UITextComponent and SpriteComponent 2022-02-06 20:12:12 -05:00
b5639e4f41 Basic WordWrap for UIText feature 2022-02-04 17:08:01 -05:00
72a941810d New UITextComponent implemented 2022-02-03 17:59:50 -05:00
40095cb8b7 Minor tweaks for debug text 2022-01-02 14:05:52 -05:00
af393e8acb a,d keys implemented and screen for 1080p 2021-12-30 13:42:55 -05:00
b9e9c04235 UI tweaks for more layers and debug 2021-11-07 20:47:58 -05:00
0cc31be0c7 Fixed Gravity to scale correctly with global scale 2021-11-05 21:54:18 -04:00
bd0c49b3ae Collider debug boxes with SDL_Rect 2021-11-03 17:37:56 -04:00
165052a617 Worked on Colliders 2021-08-25 17:11:46 -04:00
1e5beb5b88 ColliderComponent hidden prop 2021-08-16 17:36:21 -04:00
8713b76f83 added hidden property to Collider Component 2021-08-16 17:29:50 -04:00
586a774cde cleanup on aisle everywhere 2021-08-02 18:18:04 -04:00
fde99ef76b added Nic's collision pseudocode 2021-08-02 17:46:06 -04:00
363f28cb9c Fixed UIText class for non-uniform scale 2021-07-29 08:21:45 -04:00
31e16585d1 added a debug file with console output 2021-07-10 21:24:44 -04:00
10e5be7a65 Working on Collisions 2021-07-10 21:23:45 -04:00
39 changed files with 1357 additions and 424 deletions

BIN
.Makefile.un~ Normal file

Binary file not shown.

View File

@ -1,22 +1,29 @@
# Special Thanks to Job Vranish at Atomic Object for the base Makefile that was modified slightly to work with this project's needs
# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/
TARGET_EXEC ?= BeagleRescue
TARGET_EXEC ?= BeagleRescue.exe
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
BUILD_DIR ?= .\build
SRC_DIRS ?= .\src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
#SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s')
SRCS := $(shell dir .\*.cpp /s /b)
OBJS := $(SRCS:%=$(BUILD_DIR)\%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
#INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_DIRS := $(dir .\src /ad /b /s )
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -w
INCLUDE_PATHS = -IC:\mingw_dev_lib\sdl2\include\SDL2 -IC:\mingw_dev_lib\sdl_image\include\SDL2 -IC:\mingw_dev_lib\sdl_mixer\include\SDL2
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_mixer
LIBRARY_PATHS = -LC:\mingw_dev_lib\sdl2\lib -LC:\mingw_dev_lib\sdl2_image\lib -LC:\mingw_dev_lib\sdl2_mixer\lib
CPPFLAGS ?= $(INC_FLAGS) -Ilibsdl2/include -Ilibtmx-parser/src -Ilibtmx-parser/libs/tinyxml2 -MMD -MP -w
COMPILER_FLAGS = -w "-Wl,-subsystem,windows" #Quotes used to force Powershell to parse correctly
#LINKER_FLAGS = libsdl2/build/.libs/libSDL2.a libsdl2_image/.libs/libSDL2_image.a libsdl2_mixer/build/.libs/libSDL2_mixer.a libtmx-parser/libtmxparser.a
$(BUILD_DIR)\$(TARGET_EXEC): $(OBJS)
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
# $(CC) $(OBJS) -o $@ $(LDFLAGS)
$(CXX) $(OBJS) $(LINKER_FLAGS) -o $@
@ -28,18 +35,17 @@ $(BUILD_DIR)/%.s.o: %.s
# c source
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(CFLAGS) $(LINKER_FLAGS) -c $< -o $@
$(CXX) $(CPPFLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(CFLAGS) $(LINKER_FLAGS) -c $< -o $@
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LINKER_FLAGS) -c $< -o $@
$(CXX) $(CPPFLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(CXXFLAGS) $(LINKER_FLAGS) -c $< -o $@
.PHONY: clean
clean:
$(RM) -r $(BUILD_DIR)
-del -fR $(BUILD_DIR)
-include $(DEPS)

53
Makefile~ Normal file
View File

@ -0,0 +1,53 @@
# Special Thanks to Job Vranish at Atomic Object for the base Makefile that was modified slightly to work with this project's needs
# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/
TARGET_EXEC ?= BeagleRescue.exe
BUILD_DIR ?= .\build
SRC_DIRS ?= .\src
#SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s')
SRCS := $(shell dir .\*.cpp /s /b)
OBJS := $(SRCS:%=$(BUILD_DIR)\%.o)
DEPS := $(OBJS:.o=.d)
#INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_DIRS := $(dir .\src /ad /b /s )
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
INCLUDE_PATHS = -IC:\mingw_dev_lib\sdl2\include\SDL2 -IC:\mingw_dev_lib\sdl_image\include\SDL2 -IC:\mingw_dev_lib\sdl_mixer\include\SDL2
LIBRARY_PATHS = -LC:\mingw_dev_lib\sdl2\lib -LC:\mingw_dev_lib\sdl2_image\lib -LC:\mingw_dev_lib\sdl2_mixer\lib
CPPFLAGS ?= $(INC_FLAGS) -Ilibsdl2/include -Ilibtmx-parser/src -Ilibtmx-parser/libs/tinyxml2 -MMD -MP -w
COMPILER_FLAGS = -w "-Wl,-subsystem,windows" #Quotes used to force Powershell to parse correctly
all:
#LINKER_FLAGS = libsdl2/build/.libs/libSDL2.a libsdl2_image/.libs/libSDL2_image.a libsdl2_mixer/build/.libs/libSDL2_mixer.a libtmx-parser/libtmxparser.a
$(BUILD_DIR)\$(TARGET_EXEC): $(OBJS)
# $(CC) $(OBJS) -o $@ $(LDFLAGS)
$(CXX) $(OBJS) $(LINKER_FLAGS) -o $@
# assembly
$(BUILD_DIR)/%.s.o: %.s
$(MKDIR_P) $(dir $@)
$(AS) $(ASFLAGS) -c $< -o $@
# c source
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(CFLAGS) $(LINKER_FLAGS) -c $< -o $@
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(CXXFLAGS) $(LINKER_FLAGS) -c $< -o $@
.PHONY: clean
clean:
-del -fR $(BUILD_DIR)
-include $(DEPS)
MKDIR_P ?= mkdir -p

BIN
assets/maps/br-tiles.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

65
assets/maps/br-tiles.tsx Normal file
View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.8" tiledversion="1.8.2" name="br-tiles" tilewidth="16" tileheight="16" tilecount="50" columns="10">
<image source="br-tiles.png" width="160" height="80"/>
<tile id="10">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="11">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="12">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="13">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="15">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="19">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="20">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="21">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="28">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="43">
<objectgroup draworder="index" id="2">
<object id="2" x="0" y="0" width="16" height="16"/>
<object id="3" x="0.96875" y="0.875" width="14.4375" height="14.2813"/>
</objectgroup>
</tile>
<tile id="44">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="45">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
</tileset>

96
assets/maps/testmap.tmx Normal file
View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.8" tiledversion="1.8.2" orientation="orthogonal" renderorder="right-down" width="42" height="42" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="1">
<tileset firstgid="1" source="br-tiles.tsx"/>
<layer id="1" name="TileMap" width="42" height="42">
<data encoding="csv">
11,12,13,35,35,35,36,36,36,35,36,35,35,9,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,35,35,35,36,36,35,35,35,35,10,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,35,37,35,35,35,35,41,42,43,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,35,35,35,35,35,38,38,35,35,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,35,35,35,35,38,38,35,35,35,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,35,35,35,38,38,35,35,37,35,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,35,35,38,38,35,35,35,37,37,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,35,38,38,35,35,35,37,37,37,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,38,38,35,35,35,35,35,35,37,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,34,45,46,34,34,34,45,46,44,45,46,44,45,46,44,45,46,44,45,46,44,45,44,45,44,45,46,45,44,44,44,44,44,44,44,44,14,15,16,
14,15,16,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,34,34,34,41,42,43,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,34,34,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,34,34,34,34,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,34,34,34,34,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,34,34,34,34,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,44,45,46,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,44,45,46,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,11,12,13,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,11,12,13,40,40,40,40,40,14,15,16,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,17,18,19,40,40,40,40,40,14,15,16,40,40,40,40,40,40,14,15,16,
20,21,22,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,11,12,13
</data>
</layer>
<layer id="3" name="Collision" width="42" height="42" visible="0">
<data encoding="csv">
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,2,2,2,0,0,0,0,0,0,2,2,2,
2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
</data>
</layer>
</map>

File diff suppressed because one or more lines are too long

View File

View File

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.8" tiledversion="1.8.2" name="br-tiles" tilewidth="16" tileheight="16" tilecount="50" columns="10">
<image source="br-tiles.png" width="160" height="80"/>
<tile id="8"/>
<tile id="1"/>
<tile id="0"/>
<tile id="2"/>
<tile id="3"/>
<tile id="4"/>
<tile id="5"/>
<tile id="6"/>
<tile id="7"/>
<tile id="10">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="9"/>
<tile id="11">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="12">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="13">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="14"/>
<tile id="15">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="16"/>
<tile id="17"/>
<tile id="18"/>
<tile id="19">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="20">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="21">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="22"/>
<tile id="23"/>
<tile id="24"/>
<tile id="25"/>
<tile id="26"/>
<tile id="27"/>
<tile id="28">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="29"/>
<tile id="30"/>
<tile id="31"/>
<tile id="32"/>
<tile id="33"/>
<tile id="34"/>
<tile id="35"/>
<tile id="36"/>
<tile id="37"/>
<tile id="38"/>
<tile id="39"/>
<tile id="40"/>
<tile id="41"/>
<tile id="42"/>
<tile id="43">
<objectgroup draworder="index" id="2">
<object id="2" x="0" y="0" width="16" height="16"/>
<object id="3" x="0.96875" y="0.875" width="14.4375" height="14.2813"/>
</objectgroup>
</tile>
<tile id="44">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="45">
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
<tile id="46"/>
<tile id="47"/>
<tile id="48"/>
<tile id="49"/>
</tileset>

Binary file not shown.

View File

@ -0,0 +1,27 @@
checkCollision(movingRect, moveVector, solidRect)
{
// Need to make additional similar checks for each side
if (moveVector.x > 0)
{
if (movingRect.xMax < solidRect.xMin &&
movingRect.xMax + moveVector.x > solidRect.xMin)
{
xHitRatio = (solidRect.xMin - moveingRect.xMax) / moveVector.x
}
}
// Also find hit ratio of y movement and see which one will hit first if either does.
if (xHitRatio < yHitRatio)
{
yHitPosMin = movingRect.yMin + moveVector.y * xHitRatio
yHitPosMax = movingRect.yMax + moveVector.y * xHitRatio
if (yHitPosMin < solidRect.yMax &&
yHitPosMax > solidRect.yMin)
{
// We know here now where we should collide, however if we stop here the collision will feel 'sticky'
// rather than just applying the ratio to the movement vector, we want to instead slide along the collision
output.x = movingRect.xOrigin + moveVector.x * xHitRatio
output.y = movingRect.yOrigin + moveVector.y // Here is where we should split off a new movement vector to check collision again really, since it is a new direction to check, so return the hit position with a new smaller vector to 'finish' the movement.
}
}
}

7
design/debug.txt Normal file
View File

@ -0,0 +1,7 @@
Collision Stats:
Player Center
x: 871
y: 690
ObjectCollider Center
x: 872
y: 696

1
libtmx-parser Submodule

Submodule libtmx-parser added at a2b8ca09ef

View File

@ -48,6 +48,7 @@ SDL_Texture* AssetManager::GetTexture(std::string id)
}
//Sound Mixer
//Sound Clips
void AssetManager::AddSoundClip(std::string id, const char* path)
{

View File

@ -14,7 +14,6 @@
#include "../game/Vector2D.h"
#include "../ecs/ECS.h"
#include "../game/Game.hpp"
//#include <SDL2/SDL_mixer.h>
#include "MusicManager.h"
class AssetManager

View File

@ -33,7 +33,7 @@ void Map::LoadMap(std::string path, int sizeX, int sizeY, int scale)
std::fstream mapFile;
mapFile.open(path);
int srcX, srcY;
width = tSize*scale*sizeX;
height = tSize*scale*sizeY;
@ -45,7 +45,6 @@ void Map::LoadMap(std::string path, int sizeX, int sizeY, int scale)
srcY = atoi(&c) * tileSize;
mapFile.get(c);
srcX = atoi(&c) * tileSize;
AddTile(srcX, srcY, x*scaledSize, y*scaledSize);
mapFile.ignore(2,',');
}
}
@ -60,7 +59,7 @@ void Map::LoadMap(std::string path, int sizeX, int sizeY, int scale)
if (c == '1')
{
auto& tcol(manager.addEntity());
tcol.addComponent<ColliderComponent>("terrain",x*scaledSize,y*scaledSize,tileSize,scale);
tcol.addComponent<ColliderComponent>("terrain",x*scaledSize,y*scaledSize,tileSize,scale,texID);
tcol.addGroup(Game::groupColliders);
}
mapFile.ignore(2,',');

View File

@ -9,7 +9,7 @@
#define SRC_MUSICMANAGER_H_
#include "../game/Game.hpp"
#include "SDL2/SDL_mixer.h"
#include "../../libsdl2_mixer/SDL_mixer.h"
class MusicManager
{

View File

@ -1,6 +1,6 @@
{
"GameName":"Beagle Rescue",
"Developers": "Alan Youngblood, Simon Zaleski, Daniel Rinaldi",
"LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere",
"SpecialThanks":"Nic Allen, Brian Lhota"
"LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere, Bayle Jonathan",
"SpecialThanks":"Nic Allen, Brian Lhota, Rodrigo Monteiro"
}

View File

@ -14,10 +14,13 @@
#include "ECS.h"
#include "../assetmgr/TextureManager.h"
#include <iostream>
#include "../game/Vector2D.h"
class ColliderComponent : public Component
{
public:
SDL_Rect collider;
std::string tag;
@ -26,32 +29,44 @@ public:
int offsetX = 0;
int offsetY = 0;
bool hidden = true;
Vector2D center;
TransformComponent* transform;
ColliderComponent()
{
center.Zero();
}
ColliderComponent(std::string t)
{
tag = t;
// collider.x = 10;
// collider.y = 52;
// collider.w = collider.h = 12;
}
ColliderComponent(std::string t, int xpos, int ypos, int size, int scale)
ColliderComponent(std::string t, int xpos, int ypos, int size, int scale, std::string texture)
{
tag = t;
collider.x = xpos;
collider.y = ypos;
collider.w = collider.h = size*scale;
setTex(texture);
center.x = collider.x+collider.w/2;
center.y = collider.y+collider.h/2;
}
ColliderComponent(std::string t, int width, int height, bool hasOffset, int oX, int oY)
ColliderComponent(std::string t, int width, int height, bool hasOffset, int oX, int oY, std::string texture)
{
tag = t;
collider.w = width;
collider.h = height;
offsetX = oX;
offsetY = oY;
setTex(texture);
center.x = collider.x+collider.w/2;
center.y = collider.y+collider.h/2;
}
void init() override
@ -66,17 +81,6 @@ public:
tex = TextureManager::LoadTexture("assets/ColTex.png");
srcR = { 0, 0, 16, 16};
destR = { collider.x, collider.y, collider.w, collider.h };
// if(tag == "player"){
// destR = { 18, 28, 24, 24 };
// collider.w = 16;
// collider.h = 16;
// transform->height = 24;
// transform->width = 24;
// std::cout << "player collider init() ran" << std::endl;
// std::cout << "destR.w: " << destR.w << std::endl;
// std::cout << "destR.h: " << destR.h << std::endl;
// }
// Game::colliders.push_back(this);
}
void update() override
@ -85,22 +89,33 @@ public:
{
collider.x = static_cast<int>(transform->position.x+offsetX);
collider.y = static_cast<int>(transform->position.y+offsetY);
// collider.w = transform->width * transform->scale;
// collider.h = transform->height * transform->scale;
// collider.w = 12 * transform->scale;
// collider.h = 12 * transform->scale;
}
destR.x = collider.x - Game::camera.x;
destR.y = collider.y - Game::camera.y;
center.x = collider.x+collider.w/2;
center.y = collider.y+collider.h/2;
// collider.x = transform->position.x;
// collider.y = transform->position.y;
}
void draw() override
{
if(tag == "terrain")
{
TextureManager::Draw(tex, srcR, destR, SDL_FLIP_NONE);
// TextureManager::DrawCollider(destR);
// if(tag == "terrain")
// {
// TextureManager::Draw(tex, srcR, destR, SDL_FLIP_NONE);
// }
if(hidden == false)
{
// TextureManager::Draw(tex, srcR,destR,SDL_FLIP_NONE);
SDL_SetRenderDrawColor(Game::renderer,255,0,255,134);
SDL_RenderDrawRect(Game::renderer, &destR);
// SDL_RenderDrawPoint(Game::renderer, center.x, center.y);
}
}
void setTex(std::string id)
{
tex = Game::assets->GetTexture(id);
}

View File

@ -13,11 +13,10 @@
#include "SpriteComponent.h"
#include "KeyboardController.h"
#include "ColliderComponent.h"
#include "PlayerController.h"
#include "ProjectileComponent.h"
#include "TileComponent.h"
#include "UIFontComponent.h"
#include "TileMapComponent.h"
#include "UITextComponent.h"
#endif /* SRC_COMPONENTS_H_ */

View File

@ -11,21 +11,29 @@
#include "../game/Game.hpp"
#include "ECS.h"
#include "Components.h"
#include "PlayerController.h"
#include "../assetmgr/AssetManager.h"
#include "ColliderComponent.h"
#include <cmath>
// #include "../game/Vector2D.h"
class KeyboardController : public Component
{
public:
// bool keyIsAlreadyPressed[];
TransformComponent *transform;
SpriteComponent *sprite;
PlayerController *playerCtrl;
ColliderComponent *collider;
Game *game;
void init() override
{
transform = &entity->getComponent<TransformComponent>();
sprite = &entity->getComponent<SpriteComponent>();
// collider = &entity->getComponent<ColliderComponent>();
// playerCtrl = &entity->getComponent<PlayerController>();
}
void update() override
@ -34,43 +42,127 @@ public:
{
switch (Game::event.key.keysym.sym)
{
case SDLK_UP:
break;
case SDLK_DOWN:
break;
case SDLK_LEFT:
case SDLK_UP:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = -1;
if(Game::playerIsGrounded){
sprite->Play("Walk");
}
// if(transform->position.y>0){
// ====== Get Player.yTile and scan column up ScreenTilesHeight/2 on Player.xTile-1, Player.xTile, and Player.xTile+1 return first hit that is > 0 for each row.
// transform->tilePos.x
transform->velocity.y = -1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
sprite->spriteFlip = SDL_FLIP_NONE;
// }
}
break;
case SDLK_RIGHT:
case SDLK_DOWN:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 1;
if(Game::playerIsGrounded){
if(transform->position.y<Game::levelMap.h){
transform->velocity.y = 1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
int *borders;
borders = Game::predictCollisions();
sprite->spriteFlip = SDL_FLIP_NONE;
}
sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
}
break;
case SDLK_LEFT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// if (playerCtrl == NULL){
// printf("No player controller found\n");
// } else {
// printf("playerctrl.lastsafepos .x: %g .y: %g \n",playerCtrl->lastSafePosition.x,playerCtrl->lastSafePosition.y);
// playerCtrl->lastSafePosition.x = transform->position.x;
// }
// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
// printf("lastSafePos .x: %g .y: %g \n",transform->lastSafePos.x,transform->lastSafePos.y);
// Game::pTileX
// if(transform->position.x>0){
int *borders;
borders = Game::predictCollisions();
float max = borders[0];
for (int b=0;b<3;b++){
if (borders[b]>max){
max = borders[b];
}
}
float desiredMovement = -1*transform->speed*0.016; // 0.016 is the FrameTime from the Main.cpp core game loop. later, we will pass in the variable
float difference = std::abs(max-transform->position.x);
if(max*transform->scale<desiredMovement+transform->position.x){
Game::playerIsGrounded = false;
transform->velocity.x = -1;
sprite->Play("Walk");
sprite->spriteFlip = SDL_FLIP_NONE;
}else{
// transform->velocity.x = -difference/desiredMovement;
Game::playerIsGrounded = true;
transform->velocity.x = 0;
sprite->Play("Idle");
sprite->spriteFlip = SDL_FLIP_NONE;
}
}
break;
case SDLK_RIGHT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// &entity->getComponent<PlayerController>().setLastSafePos(Vector2D(transform->position.x,transform->position.y));
// playerCtrl->lastSafePosition.x = transform->position.x;
// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
// =========
// Do a pre-check for tile-based smooth algorithm
// =========
// int intersectionTileX = collider->center.x;
if(transform->position.x<Game::levelMap.w){
transform->velocity.x = 1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
}
}
break;
// case SDLK_a:
// if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// transform->velocity.x = -1;
// if(Game::playerIsGrounded){
// sprite->Play("Walk");
// }
// sprite->spriteFlip = SDL_FLIP_NONE;
// }
// break;
// case SDLK_d:
// if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// transform->velocity.x = 1;
// if(Game::playerIsGrounded){
// sprite->Play("Walk");
// }
// sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
// }
// break;
case SDLK_k:
if (!Game::debugCollisionBoxes)
{ Game::debugCollisionBoxes = true; }
else
{Game::debugCollisionBoxes = false; }
if (!Game::debugMenu)
{ Game::debugMenu = true; }
else
{ Game::debugMenu = false; }
break;
case SDLK_j:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0)
{
Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0);
if(Game::playerIsGrounded){
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0)
{
Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0);
}
// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
Game::gravityOnPlayer = true;
sprite->Play("Jump");
transform->velocity.y = -2;
}
Game::gravityOnPlayer = true;
sprite->Play("Jump");
transform->velocity.y = -2;
}
break;
case SDLK_u:
@ -83,39 +175,71 @@ public:
break;
}
}
// ===============================================
// ON KEY UP
// ===============================================
else if (Game::event.type == SDL_KEYUP)
{
switch (Game::event.key.keysym.sym)
{
case SDLK_UP:
// transform->velocity.y = 0;
// sprite->Play("idle");
// sprite->spriteFlip = SDL_FLIP_NONE;
break;
case SDLK_DOWN:
// transform->velocity.y = 0;
// sprite->Play("idle");
// sprite->spriteFlip = SDL_FLIP_NONE;
break;
case SDLK_LEFT:
case SDLK_LEFT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 0;
sprite->Play("Idle");
if (!Game::gravityOnPlayer){
Game::gravityOnPlayer = true;
// sprite->Play("Fall");
}
Game::playerIsGrounded = false;
// if (!Game::gravityOnPlayer){
// Game::gravityOnPlayer = true;
// // sprite->Play("Fall");
// }
}
break;
case SDLK_RIGHT:
case SDLK_RIGHT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 0;
sprite->Play("Idle");
if (!Game::gravityOnPlayer){
Game::gravityOnPlayer = true;
}
// if (!Game::gravityOnPlayer){
// Game::gravityOnPlayer = true;
// }
}
break;
case SDLK_UP:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.y = 0;
sprite->Play("Idle");
// if (!Game::gravityOnPlayer){
// Game::gravityOnPlayer = true;
// // sprite->Play("Fall");
// }
}
break;
case SDLK_DOWN:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.y = 0;
sprite->Play("Idle");
// if (!Game::gravityOnPlayer){
// Game::gravityOnPlayer = true;
// }
}
break;
// case SDLK_a:
// if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// transform->velocity.x = 0;
// sprite->Play("Idle");
// if (!Game::gravityOnPlayer){
// Game::gravityOnPlayer = true;
// // sprite->Play("Fall");
// }
// }
// break;
// case SDLK_d:
// if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// transform->velocity.x = 0;
// sprite->Play("Idle");
// if (!Game::gravityOnPlayer){
// Game::gravityOnPlayer = true;
// }
// }
// break;
case SDLK_k:
break;
case SDLK_j:
@ -129,6 +253,13 @@ public:
break;
case SDLK_i:
break;
// case SDLK_d:
// if (Game::debugMenu == false){
// Game::debugMenu = true;}
// else {
// Game::debugMenu = false;
// }
// break;
case SDLK_ESCAPE: // exit the game when Escape pressed
Game::isRunning = false;
break;

View File

@ -0,0 +1,77 @@
/*
* PlayerController.h
*
* Created on: Aug 27, 2021
* Author: ayoungblood
*/
#ifndef SRC_ECS_PlayerController_H_
#define SRC_ECS_PlayerController_H_
#include "Components.h"
#include "../game/Vector2D.h"
class PlayerController : public Component
{
public:
Vector2D lastSafePosition;
float jumpTime;
float jumpForce;
bool isJumping;
bool isGrounded;
PlayerController()
{
jumpTime = 0.0f;
jumpForce = 0.0f;
bool isJumping = false;
bool isGrounded = false;
lastSafePosition = Vector2D();
}
PlayerController(float jumpT, float jumpF, bool jumping, bool grounded, Vector2D lastPos)
{
jumpTime = jumpT;
jumpForce = jumpF;
bool isJumping = jumping;
bool isGrounded = grounded;
lastSafePosition = lastPos;
}
~PlayerController()
{
}
void init() override
{
lastSafePosition.Zero();
}
void update() override
{
}
void setLastSafePos(Vector2D prevSafePos)
{
lastSafePosition = prevSafePos;
}
// void init() override
// {
//
// }
};
#endif /* SRC_ECS_PlayerController_H_ */

View File

@ -24,7 +24,6 @@ public:
{
transform = &entity->getComponent<TransformComponent>();
transform->velocity = velocity;
// std::cout << transform->position << std::endl;
}
void update() override

View File

@ -8,10 +8,6 @@
#ifndef SRC_ECS_SPRITECOMPONENT_H_
#define SRC_ECS_SPRITECOMPONENT_H_
#define ASCII_START_IDX 32
#define ASCII_COUNT 96
#define ASCII_ROW_COUNT 16
#include "Components.h"
#include "SDL2/SDL.h"
#include "../assetmgr/TextureManager.h"
@ -69,20 +65,17 @@ public:
{
spriteType = sType;
if(sType == spriteAnimation)
{
std::string bogusPath = "src/config/credits.json";
std::ifstream fin(bogusPath);
{
std::ifstream fin(json);
if(fin.is_open()){
std::cout<<"file is open"<<std::endl;
} else {
std::cout<<"file is NOT open"<<std::endl;
std::cout<<"json file is NOT open"<<std::endl;
}
if(fin.fail()){
std::cout<<"file open fail"<<std::endl;
std::cout<<"json file open fail"<<std::endl;
} else{
std::cout<<"file open success"<<std::endl;
}
std::ifstream jsonText(json);
@ -92,35 +85,20 @@ public:
cJSON * animJson = cJSON_Parse(aJson.c_str());
cJSON * meta = cJSON_GetObjectItem(animJson, "meta");
cJSON * frameTags = cJSON_GetObjectItem(meta,"frameTags");
// printf("frameTags:\n%s\n",cJSON_Print(frameTags));
int tagsCount = cJSON_GetArraySize(frameTags);
// printf("number of tags: \n%d\n",tagsCount);
// cJSON * arrItem = cJSON_GetArrayItem(frameTags,0);
// cJSON * animItem = cJSON_GetArrayItem(frameTags, 0);
// printf("Animation item: \n%s\n",cJSON_Print(animItem));
// printf("arrItem: \n%s\n",cJSON_Print(arrItem));
for (int t = 0; t < tagsCount; t++)
{
// printf("Tag: \n%d\n",t);
// printf("tag number: \n%d\n",t);
cJSON * animItem = cJSON_GetArrayItem(frameTags,t);
// printf("Animation item: \n%s\n",cJSON_Print(animItem));
cJSON * nameJson = cJSON_GetObjectItem(animItem, "name");
const char * name = cJSON_Print(nameJson);
int fromFrame = cJSON_GetObjectItem(animItem, "from")->valueint;
int toFrame = cJSON_GetObjectItem(animItem, "to")->valueint;
Animation anim = Animation(fromFrame,toFrame,100);
animations.emplace(name, anim);
// printf("Playing animation named: %s fromFrame:%d toFrame:%d \n",name,fromFrame,toFrame);
Play(name);
}
// if(!animations.empty()){
// printf("animations found!\n");
// }else{
// printf("No animations\n");
// }
// Play("idle");
Animation idle = Animation(0,3,100);
animations.emplace("Idle", idle);
Animation walk = Animation(1,3,100);
@ -134,19 +112,6 @@ public:
setTex(id);
}
SpriteComponent(std::string id, SpriteType sType, char fontLetter, int letterW, int letterH, int letterScale)
{
spriteType = sType;
setTex(id);
// text = isText;
letter = fontLetter;
letterWidth = letterW;
letterHeight = letterH;
scale = letterScale;
destRect.w = letterW*letterScale;
destRect.h = letterH*letterScale;
}
SpriteComponent(std::string id, SpriteType sType, SDL_Rect srcR, SDL_Rect destR)
{
spriteType = sType;
@ -171,14 +136,6 @@ public:
switch(spriteType)
{
case spriteText:
srcRect.x = ((letter-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth;
srcRect.y = ((letter-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight;
srcRect.w = letterWidth;
srcRect.h = letterHeight;
destRect.w = letterWidth*scale;
destRect.h = letterHeight*scale;
break;
case spriteUIL0: case spriteUIL1: case spriteBackground:
break;
case spriteActor: case spriteAnimation: case spriteAtlas: case spriteObject: case spriteTileMap:

132
src/ecs/TileMapComponent.h Normal file
View File

@ -0,0 +1,132 @@
/*
* TileMapComponent.h
*
* Created on: Mar 21, 2020
* Author: ayoungblood
*/
#ifndef SRC_ECS_TILEMAPCOMPONENT_H_
#define SRC_ECS_TILEMAPCOMPONENT_H_
#include "ECS.h"
#include "../../libsdl2/include/SDL.h"
#include "../assetmgr/AssetManager.h"
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
#include "tmxparser.h"
class TileMapComponent : public Component
{
public:
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
tmxparser::TmxMap map;
int globalScale;
std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects;
std::vector<std::tuple<int,int>> initialPositions;
int tileSetTotal;
std::vector<std::vector<int>> colliders;
int tilesWide;
int tilesHigh;
int tileWidth;
TileMapComponent() = default;
~TileMapComponent()
{
SDL_DestroyTexture(texture);
}
TileMapComponent(tmxparser::TmxMap loadedMap, int gScale, int offsetX, int offsetY)
{
map = loadedMap;
std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str());
setTex(map.tilesetCollection[0].name);
globalScale = gScale;
colliders.resize(map.height, std::vector<int>(map.width, 0));
tilesWide = map.width;
tilesHigh = map.height;
tileWidth = map.tileWidth;
// =========== Setup Tile Set ===========
tileSetTotal = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
tileSet.resize(tileSetTotal);
for (int r=0;r<map.tilesetCollection[0].rowCount;r++){
for (int c=0;c<map.tilesetCollection[0].colCount;c++){
srcRect.x = c*map.tilesetCollection[0].tileWidth;
srcRect.y = r*map.tilesetCollection[0].tileHeight;
srcRect.w = srcRect.h = map.tileWidth;
int element = r*map.tilesetCollection[0].colCount+c;
tileSet[element] = srcRect;
}
}
// =========== Setup Tile Map ============
destRects.resize(map.width*map.height);
initialPositions.resize(map.width*map.height);
for (int r = 0;r<map.height;r++){
for (int c = 0;c<map.width;c++){
int elem = c+r*map.width;
SDL_Rect thisRect = SDL_Rect();
thisRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
thisRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
thisRect.w = thisRect.h = map.tileWidth * globalScale;
std::tuple<int,int> ogPos = std::make_tuple(thisRect.x,thisRect.y);
initialPositions[elem] = ogPos;
thisRect.x = thisRect.x-offsetX*globalScale;
thisRect.y = thisRect.y-offsetY*globalScale;
destRects[elem] = thisRect;
colliders[r][c] = map.layerCollection[1].tiles[elem].gid;
// std::cout << "colliders[" << std::to_string(r) << "][" << std::to_string(c) << "]= " << std::to_string(colliders[r][c]) << std::endl;
}
}
destRect.w = destRect.h = map.tileWidth * gScale;
}
void update() override
{
// if (Game::gsm->currentState == GameStateManager::ST_COREGAME){
for (int i=0;i<destRects.size();i++){
destRects[i].x = std::get<0>(initialPositions[i]) - Game::camera.x;
destRects[i].y = std::get<1>(initialPositions[i]) - Game::camera.y;
}
// }
}
void draw() override
{
//iterate through rows and columns of the map to draw the tiles
// First cycle through rows
for (int r = 0;r<map.height;r++){
// Next cycle through each column or tile in that row:
for (int c = 0;c<map.width;c++){
int i = r*map.width+c;
int elem = c+r*map.width;
int tileToDraw = map.layerCollection[0].tiles[i].gid-1;
TextureManager::Draw(texture, tileSet[tileToDraw], destRects[elem], SDL_FLIP_NONE);
if (Game::debugMenu){
if (map.layerCollection[1].tiles[i].gid != 0) {
SDL_SetRenderDrawColor(Game::renderer,255,0,255,134);
SDL_RenderDrawRect(Game::renderer, &destRects[elem]);
}
}
}
}
}
void setTex(std::string id)
{
texture = Game::assets->GetTexture(id);
}
};
#endif /* SRC_ECS_TILEMAPCOMPONENT_H_ */

View File

@ -18,11 +18,13 @@ public:
Vector2D position;
Vector2D velocity;
// Vector2D lastSafePos;
Vector2D tilePos;
int height = 40;
int width = 30;
int scale = 1;
int speed = 2;
int speed = 1;
TransformComponent()
{
@ -37,10 +39,10 @@ public:
speed = speed*sc;
}
TransformComponent(float x, float y)
{
position.Zero();
}
// TransformComponent(float x, float y)
// {
// position.Zero();
// }
TransformComponent(int x, int y, int w, int h, int sc)
{
@ -52,16 +54,32 @@ public:
speed = speed*sc;
}
TransformComponent(int x, int y, int w, int h, int sc, int spd)
{
position.x = x;
position.y = y;
width = w;
height = h;
scale = sc;
speed = spd*sc;
}
void init() override
{
velocity.Zero();
tilePos.Zero();
// lastSafePos.Zero();
}
void update() override
{
position.x += velocity.x * speed;
position.y += velocity.y * speed;
}
void updateTilePosition(int x,int y){
tilePos.x = x;
tilePos.y = y;
}
};

View File

@ -1,83 +0,0 @@
/*
* UIFontComponent.h
*
* Created on: May 14, 2020
* Author: ayoungblood
*/
#ifndef SRC_ECS_UIFONTCOMPONENT_H_
#define SRC_ECS_UIFONTCOMPONENT_H_
#define ASCII_START_IDX 32
#define ASCII_COUNT 96
#define ASCII_ROW_COUNT 16
#include "Components.h"
#include "SDL2/SDL.h"
#include "../assetmgr/AssetManager.h"
class UIFontComponent : public Component
{
private:
// SDL_Texture * texture;
// Font dimensions
int _LetterWidth;
int _LetterHeight;
// Track current letter on texture
SDL_Rect _LetterClips[ASCII_COUNT];
public:
// SDL_RendererFlip spriteFlip = SDL_FLIP_NONE;
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
Vector2D position;
// UIFontComponent() = default;
UIFontComponent(std::string id, int letterW, int letterH, int xpos, int ypos, int scale)
{
texture = Game::assets->GetTexture(id);
srcRect = UIFontComponent::SetCharClips(texture, xpos, ypos);
position.x = xpos;
position.y = ypos;
destRect.x = xpos;
destRect.y = ypos;
destRect.w = letterW * scale;
destRect.h = letterH * scale;
}
~UIFontComponent()
{
SDL_DestroyTexture(texture);
}
void update() override
{
}
void draw() override
{
TextureManager::Draw(texture, srcRect, destRect, SDL_FLIP_NONE);
}
SDL_Rect SetCharClips(SDL_Texture* fontTex, int x, int y)
{
SDL_Rect letterClip;
for (int i = 0; i < ASCII_COUNT; ++i)
{
_LetterClips[i].x = x + ((i % ASCII_ROW_COUNT) * _LetterWidth);
_LetterClips[i].y = y + ((i / ASCII_ROW_COUNT) * _LetterHeight);
_LetterClips[i].w = _LetterWidth;
_LetterClips[i].h = _LetterHeight;
letterClip = _LetterClips[i];
}
return letterClip;
}
};
#endif /* SRC_ECS_UIFONTCOMPONENT_H_ */

101
src/ecs/UITextComponent.h Normal file
View File

@ -0,0 +1,101 @@
/*
* UITextComponent.h
*
* Created on: Feb 22, 2020
* Author: ayoungblood
*/
#ifndef SRC_ECS_UITEXTCOMPONENT_H_
#define SRC_ECS_UITEXTCOMPONENT_H_
#define ASCII_START_IDX 32
#define ASCII_COUNT 96
#define ASCII_ROW_COUNT 16
#include "Components.h"
#include "SDL2/SDL.h"
#include "../assetmgr/TextureManager.h"
#include "../assetmgr/AssetManager.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <tuple>
#include <cmath>
class UITextComponent : public Component
{
private:
TransformComponent *transform;
SDL_Texture *texture;
SDL_Rect srcRect, destRect;
std::string text;
int letterWidth, letterHeight;
int scale = 1;
SDL_RendererFlip spriteFlip = SDL_FLIP_NONE;
std::tuple <SDL_Rect, SDL_Rect> letter;
public:
UITextComponent(std::string id, std::string textToPrint, int letterW, int letterH, int letterScale)
{
setTex(id);
text = textToPrint;
letterWidth = letterW;
letterHeight = letterH;
scale = letterScale;
}
~UITextComponent()
{
SDL_DestroyTexture(texture);
}
void setTex(std::string id)
{
texture = Game::assets->GetTexture(id);
}
void init() override
{
transform = &entity->getComponent<TransformComponent>();
}
void update() override
{
destRect.x = static_cast<int>(transform->position.x);
destRect.y = static_cast<int>(transform->position.y);
}
void draw() override
{
for (int l = 0; l < text.length(); l++)
{
std::tuple<SDL_Rect, SDL_Rect> lttr = getLetterTexture(text[l],l);
TextureManager::Draw(texture, std::get<0>(lttr), std::get<1>(lttr), spriteFlip);
}
}
std::tuple<SDL_Rect, SDL_Rect> getLetterTexture(char currentLetter, int i)
{
std::tuple<SDL_Rect, SDL_Rect> letterTuple;
int charsPerLine = std::floor(transform->width/(letterWidth*scale));
srcRect.x = ((currentLetter-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth;
srcRect.y = ((currentLetter-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight;
srcRect.w = letterWidth;
srcRect.h = letterHeight;
destRect.x = static_cast<int>(transform->position.x)*scale+(i%charsPerLine)*letterWidth*scale;
destRect.y = static_cast<int>(transform->position.y)*scale+std::floor(i/charsPerLine)*letterHeight*scale*1.1;
destRect.w = letterWidth*scale;
destRect.h = letterHeight*scale;
letterTuple = std::make_tuple(srcRect,destRect);
return letterTuple;
}
void updateString(std::string newString)
{
text = newString;
}
};
#endif /* SRC_ECS_UITEXTCOMPONENT_H_ */

View File

@ -11,15 +11,14 @@
bool Collision::AABB(const SDL_Rect& recA, const SDL_Rect& recB)
{
if(
recA.x + recA.w >= recB.x &&
recB.x + recB.w >= recA.x &&
recA.y + recA.h >= recB.y &&
recB.y + recB.h >= recA.y
recA.x + recA.w > recB.x &&
recB.x + recB.w > recA.x &&
recA.y + recA.h > recB.y &&
recB.y + recB.h > recA.y
)
{
{
return true;
}
return false;
}
@ -27,7 +26,6 @@ bool Collision::AABB(const ColliderComponent& colA, const ColliderComponent& col
{
if(AABB(colA.collider, colB.collider))
{
// std::cout << colA.tag << " hit: " << colB.tag << std::endl;
return true;
}
else

View File

@ -9,6 +9,7 @@
#define SRC_COLLISION_H_
#include <SDL2/SDL.h>
// #include "Vector2D.h"
class ColliderComponent;

View File

@ -10,7 +10,7 @@
#include "Game.hpp"
#include "../assetmgr/TextureManager.h"
#include "../assetmgr/Map.h"
// #include "../assetmgr/Map.h"
#include "../ecs/Components.h"
#include "Collision.h"
#include <string>
@ -18,47 +18,74 @@
#include <fstream>
#include <sstream>
#include "../assetmgr/AssetManager.h"
#include "../ui/UIText.h"
// #include "../ui/UIText.h"
#include "../ui/UINineSlice.h"
#include "../cjson/cJSON.h"
#include <cmath>
Map* map;
// tmxparser::TmxMap map;
Manager manager;
UIText* text;
UINineSlice* my9Slice;
UIText* scoreboardText;
UINineSlice* scoreboard9Slice;
UINineSlice* debugBox;
GameStateManager* Game::gsm = new GameStateManager();
SDL_Renderer* Game::renderer = nullptr;
SDL_Event Game::event;
SDL_Rect Game::camera;
SDL_Rect Game::levelMap;
AssetManager* Game::assets = new AssetManager(&manager);
bool Game::isRunning = false;
bool Game::debugMenu = false;
auto& player(manager.addEntity());
Vector2D Game::playerPosition;
Vector2D Game::pVel;
// auto& enemy(manager.addEntity());
auto& puppy(manager.addEntity());
// auto& scoreboard(manager.addEntity());
auto& uiInfo(manager.addEntity());
auto& uiJumpInfo(manager.addEntity());
auto& uiCamXInfo(manager.addEntity());
auto& uiCamYInfo(manager.addEntity());
auto& uiPlayerXInfo(manager.addEntity());
auto& uiPlayerYInfo(manager.addEntity());
auto& uiBoundary1Info(manager.addEntity());
auto& uiBoundary2Info(manager.addEntity());
auto& uiBoundary3Info(manager.addEntity());
auto& uiTextInstructions(manager.addEntity());
auto& gameScene(manager.addEntity());
bool Game::debugCollisionBoxes = false;
bool Game::gravityOnPlayer = true;
bool Game::playerIsGrounded = false;
bool Game::playerIsJumping = false;
int gScale = 0;
int last_time;
int current_time;
int diff_time;
int Game::pTileX = 0;
int Game::pTileY = 0;
std::string Game::BoolToString(bool b) {
std::string myString;
if (b) {
myString = "true";
} else {
myString = "false";
}
return myString;
}
Game::Game() {
// TODO Auto-generated constructor stub
@ -82,6 +109,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
SDL_SetWindowBordered(window,SDL_FALSE);
if(!window)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Couldn't create window: %s", SDL_GetError());
@ -104,13 +132,32 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize SDL audio mixer!, Error: %s", SDL_GetError());
}
// current_time = SDL_GetTicks();
assets->AddTexture("terrain", "assets/textures/tiles/br-tiles.png");
// Trying out the halsafar/libtmx-parser
tmxparser::TmxReturn error;
tmxparser::TmxMap map;
error = tmxparser::parseFromFile("assets/maps/testmapb64.tmx", &map, "assets/textures/tiles/");
if (!error)
{
// printf("Yay! Tile map loaded with no errors.\n");
tmxparser::TmxLayer layer = map.layerCollection[0];
levelMap.x = 0;
levelMap.y = 0;
levelMap.w = map.width*map.tileWidth*gScale;
levelMap.h = map.height*map.tileHeight*gScale;
// std::cout << "Map Width Tiles: " << map.width << " Map Tile Width: " << map.tileWidth <<std::endl;
} else {
std::cout << "Encountered error loading map file: " << error << std::endl;
}
// std::cout << "levelMap.w - camera.w: " << levelMap.w-camera.w << std::endl;
assets->AddTexture("player", "assets/textures/actors/firefighter.png");
assets->AddTexture("font", "assets/textures/ui/ui-font-cloud-sans.png");
assets->AddTexture("textBox", "assets/textures/ui/ui-element-cloud.png");
assets->AddTexture("puppy","assets/textures/actors/beaglepuppy.png");
assets->AddTexture("collider","assets/textures/ColTex.png");
assets->AddMusicTrack("simonZ","assets/audio/music/sillypuppy.ogg");
assets->AddMusicTrack("simonZ","assets/audio/music/victory.ogg");
@ -119,57 +166,94 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
assets->AddSoundClip("bark1","assets/audio/sfx/Bark1.wav");
assets->AddSoundClip("bark2","assets/audio/sfx/Bark2.wav");
map = new Map("terrain",globalScale,16);
std::string myText = "Find lost puppies!\nThey need your help!";
text = new UIText(myText, "font", 0, 0, 8, 12, globalScale);
text->ParseString(myText, 12, 22, globalScale, "text");
// map = new Map("terrain",globalScale,16);
// std::string myText = "Find lost puppies!\nThey need your help!";
std::string myText = "Press U to Start";
uiTextInstructions.addComponent<TransformComponent>(18,22,138*gScale,20*gScale,gScale);
uiTextInstructions.addComponent<UITextComponent>("font",myText,8,12,gScale);
uiTextInstructions.addGroup(groupUI_Layer1);
SDL_Rect myDestRect = SDL_Rect();
myDestRect.x = 12;
myDestRect.y = 8;
myDestRect.w = 160;
myDestRect.h = 40;
my9Slice = new UINineSlice("textBox");
my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale);
my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0);
// std::cout << "camera.w " << camera.w << std::endl;
uiInfo.addComponent<TransformComponent>(camera.w/gScale-94,10,72*gScale,96*gScale,gScale);
// uiInfo.addComponent<UITextComponent>("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1);
uiInfo.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : coll: Px2 bnd ", 8, 12, gScale);
uiInfo.addGroup(groupUI_Layer3);
uiCamXInfo.addComponent<TransformComponent>(camera.w/gScale-48,23,40*gScale,12*gScale,gScale);
uiCamXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiCamXInfo.addGroup(groupUI_Layer3);
uiCamYInfo.addComponent<TransformComponent>(camera.w/gScale-48,36,40*gScale,12*gScale,gScale);
uiCamYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiCamYInfo.addGroup(groupUI_Layer3);
uiPlayerXInfo.addComponent<TransformComponent>(camera.w/gScale-49,49,40*gScale,12*gScale,gScale);
uiPlayerXInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiPlayerXInfo.addGroup(groupUI_Layer3);
uiPlayerYInfo.addComponent<TransformComponent>(camera.w/gScale-48,62,40*gScale,12*gScale,gScale);
uiPlayerYInfo.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiPlayerYInfo.addGroup(groupUI_Layer3);
uiBoundary1Info.addComponent<TransformComponent>(camera.w/gScale-48,75,128*gScale,12*gScale,gScale);
uiBoundary1Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary1Info.addGroup(groupUI_Layer3);
uiBoundary2Info.addComponent<TransformComponent>(camera.w/gScale-64,88,128*gScale,12*gScale,gScale);
uiBoundary2Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary2Info.addGroup(groupUI_Layer3);
uiBoundary3Info.addComponent<TransformComponent>(camera.w/gScale-64,101,128*gScale,12*gScale,gScale);
uiBoundary3Info.addComponent<UITextComponent>("font", "nan", 8, 12, gScale);
uiBoundary3Info.addGroup(groupUI_Layer3);
// debug UI box
SDL_Rect debugBoxRect = SDL_Rect();
debugBoxRect.x = camera.w-(100*gScale);
debugBoxRect.y = 4*gScale;
debugBoxRect.w = 98*gScale;
debugBoxRect.h = 112*gScale;
debugBox = new UINineSlice("textBox");
debugBox->MakeSlices("textBox",32,32,14,16,14,16,debugBoxRect,1,Game::groupUI_Layer2);
//ecs implementation
map->LoadMap("assets/maps/br-map-color.txt",70,45, globalScale);
player.addComponent<TransformComponent>(860*globalScale,630*globalScale,22,42,globalScale);
// player.addComponent<TransformComponent>(150*globalScale,80*globalScale,40,40,globalScale);
// player.addComponent<TransformComponent>(860*globalScale,640*globalScale,22,42,globalScale);
player.addComponent<TransformComponent>(150*gScale,100*gScale,22,42,globalScale,3); // 180,120
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
// player.addComponent<PlayerController>(0.0,0.0,false,false,Vector2D().Zero());
player.addComponent<ColliderComponent>("player",16*globalScale,32*globalScale, true, 2*globalScale,10*globalScale, "collider");
player.addComponent<KeyboardController>();
player.addComponent<ColliderComponent>("player",8*globalScale,8*globalScale, true, 7*globalScale,36*globalScale);
player.addGroup(groupPlayers);
gameScene.addComponent<TileMapComponent>(map,gScale,player.getComponent<TransformComponent>().position.x+player.getComponent<TransformComponent>().width/2,player.getComponent<TransformComponent>().position.y+player.getComponent<TransformComponent>().height/2); //150,100
gameScene.addGroup(groupMap);
playerPosition = Vector2D().Zero();
pVel = Vector2D().Zero();
puppy.addComponent<TransformComponent>(1024*globalScale,210*globalScale,36,30,globalScale);
puppy.addComponent<SpriteComponent>("puppy", SpriteComponent::spriteObject);
puppy.addGroup(groupObjects);
// const char* strA = "r";
// const char* strB = "r";
//
// if (strcmp(strA,strB)==0)
// {
// printf("strcomp evaluated to true");
// } else {
// printf("strcomp false or not evaluated");
// }
// enemy.addComponent<TransformComponent>(180*globalScale,180*globalScale,32,32,globalScale);
// enemy.addComponent<SpriteComponent>("robber", SpriteComponent::spriteAnimation, "assets/textures/actors/robberrodent.json");
// enemy.addGroup(groupEnemies);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't Initialize SDL: %s", SDL_GetError());
isRunning = false;
}
}
//camera.x = camera.x*globalScale;
// ====== Setup groups
auto& tiles(manager.getGroup(Game::groupMap));
auto& players(manager.getGroup(Game::groupPlayers));
auto& colliders(manager.getGroup(Game::groupColliders));
@ -178,7 +262,8 @@ auto& objects(manager.getGroup(Game::groupObjects));
// auto& projectiles(manager.getGroup(Game::groupProjectiles));
auto& gui(manager.getGroup(Game::groupUI_Layer0));
auto& uiText(manager.getGroup(Game::groupUI_Layer1));
auto& debugBG(manager.getGroup(Game::groupUI_Layer2));
auto& debugText(manager.getGroup(Game::groupUI_Layer3));
void Game::handleEvents()
{
@ -207,11 +292,10 @@ void Game::update()
// const char* gameOverText = "Game Over";
// }
if (Mix_PlayingMusic() == 0 && gsm->currentState == GameStateManager::ST_COREGAME)
{
// std::cout << "Play Music Now" << std::endl;
Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
// Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
}
if (Mix_PlayingMusic() != 0 && gsm->currentState != GameStateManager::ST_COREGAME)
{
@ -230,20 +314,23 @@ void Game::update()
SDL_Rect cCol = c->getComponent<ColliderComponent>().collider;
if(Collision::AABB(cCol, playerCol))
{
// printf("Collision detected!\nplayerIsGrounded:%d if 0: idle plays\n",playerIsGrounded);
if(!playerIsGrounded){
player.getComponent<SpriteComponent>().Play("Idle");
}
gravityOnPlayer = false;
playerIsGrounded = true;
// if(!playerIsGrounded){
// player.getComponent<SpriteComponent>().Play("Idle");
// }
// playerIsGrounded = true;
// player.getComponent<TransformComponent>().position.y = player.getComponent<TransformComponent>().lastSafePos.y;
// gravityOnPlayer = false;
}
}
// Gravity
if (gravityOnPlayer){
player.getComponent<TransformComponent>().position.y += 10;
}
// if (gravityOnPlayer){
// player.getComponent<TransformComponent>().position.y += 3*gScale;
// uiJumpInfo.getComponent<UITextComponent>().updateString("true");
// } else {
// uiJumpInfo.getComponent<UITextComponent>().updateString("false");
// }
// for(auto& p: projectiles)
// {
// if(Collision::AABB(player.getComponent<ColliderComponent>().collider, p->getComponent<ColliderComponent>().collider))
@ -253,17 +340,81 @@ void Game::update()
// }
// }
camera.x = player.getComponent<TransformComponent>().position.x - camera.w/2;
camera.y = player.getComponent<TransformComponent>().position.y - camera.h/2;
auto& tileMap = gameScene.getComponent<TileMapComponent>();
camera.x = player.getComponent<TransformComponent>().position.x - camera.w/2 + player.getComponent<TransformComponent>().width/2;
camera.y = player.getComponent<TransformComponent>().position.y - camera.h/2 + player.getComponent<TransformComponent>().height/2;
pTileX = (player.getComponent<TransformComponent>().position.x+player.getComponent<TransformComponent>().width/2)/gScale/tileMap.tileWidth;
pTileY = (player.getComponent<TransformComponent>().position.y+player.getComponent<TransformComponent>().height/2)/gScale/tileMap.tileWidth;
player.getComponent<TransformComponent>().updateTilePosition(pTileX,pTileY);
uiCamXInfo.getComponent<UITextComponent>().updateString(std::to_string((int)player.getComponent<TransformComponent>().tilePos.x));
uiCamYInfo.getComponent<UITextComponent>().updateString(std::to_string((int)player.getComponent<TransformComponent>().tilePos.y));
int playerX = player.getComponent<TransformComponent>().position.x;
int playerY = player.getComponent<TransformComponent>().position.y;
uiPlayerXInfo.getComponent<UITextComponent>().updateString(std::to_string(playerX));
uiPlayerYInfo.getComponent<UITextComponent>().updateString(std::to_string(playerY));
int * foundBoundaries = predictCollisions();
int max = foundBoundaries[0];
for (int b=0;b<3;b++){
if (foundBoundaries[b]>max){
max = foundBoundaries[b];
}
}
float desiredMovementX = -1*player.getComponent<TransformComponent>().speed*0.016;
// int desiredMovementY = player.getComponent<TransformComponent>().velocity.y*player.getComponent<TransformComponent>().speed*player.getComponent<TransformComponent>().scale;
// uiBoundary1Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[0]));
// uiBoundary1Info.getComponent<UITextComponent>().updateString(std::to_string((float)desiredMovementX));
if (playerIsGrounded) {
uiBoundary1Info.getComponent<UITextComponent>().updateString("yes");
} else {
uiBoundary1Info.getComponent<UITextComponent>().updateString("no");
}
// uiBoundary2Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[1]));
float difference = player.getComponent<TransformComponent>().position.x+desiredMovementX;
uiBoundary2Info.getComponent<UITextComponent>().updateString(std::to_string((float)difference));
uiBoundary3Info.getComponent<UITextComponent>().updateString(std::to_string((int)(max*gScale)));
// uiBoundary3Info.getComponent<UITextComponent>().updateString(std::to_string((int)foundBoundaries[2]));
playerPosition.x = playerX;
playerPosition.y = playerY;
pVel.x = player.getComponent<TransformComponent>().velocity.x;
pVel.y = player.getComponent<TransformComponent>().velocity.y;
if(camera.x < 0)
camera.x = 0;
if (camera.y < 0)
camera.y = 0;
if (camera.x > map->width-camera.w)
camera.x = map->width-camera.w;
if (camera.y > map->height-camera.h)
camera.y = map->height-camera.h;
if (camera.x > levelMap.w-camera.w)
camera.x = levelMap.w-camera.w;
if (camera.y > levelMap.h-camera.h)
camera.y = levelMap.h-camera.h;
if (Game::debugCollisionBoxes)
{
for (auto& c: colliders)
{
c->getComponent<ColliderComponent>().hidden = false;
}
for (auto& p: players)
{
p->getComponent<ColliderComponent>().hidden = false;
}
} else {
for (auto& c: colliders)
{
c->getComponent<ColliderComponent>().hidden = true;
}
for (auto& p: players)
{
p->getComponent<ColliderComponent>().hidden = true;
}
}
}
void Game::render()
@ -277,14 +428,12 @@ void Game::render()
{
t->draw();
}
if (Game::debugCollisionBoxes)
for (auto& c : colliders)
{
for (auto& c : colliders)
{
c->draw();
}
c->draw();
}
for (auto& o : objects)
{
o->draw();
@ -304,13 +453,23 @@ void Game::render()
{
guiElement->draw();
}
for (auto& letter : uiText)
for (auto& text : uiText)
{
letter->draw();
text->draw();
}
}
if (debugMenu)
{
for (auto& guiElement : debugBG)
{
guiElement->draw();
}
for (auto& text : debugText)
{
text->draw();
}
}
SDL_RenderPresent(renderer);
}
@ -324,14 +483,84 @@ void Game::clean()
printf("Game Cleaned\n");
}
void Game::printDebug(char* debugInfo)
{
printf("%s",debugInfo);
printf("\n");
}
void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue)
{
SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
SDL_SetRenderDrawColor(renderer, red, green, blue, 200);
SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
}
int * Game::predictCollisions(){
static int boundaries[3];
// ===== LEFT =====
if (player.getComponent<TransformComponent>().velocity.x<0){
// ====== For Each Row ====
int i = 0;
for (int r=Game::pTileY-1;r<=Game::pTileY+1;r++){
// ====== For Each Tile (Column) =====
if(r<0){r=0;}
for (int c=Game::pTileX;c>Game::pTileX-Game::camera.w/gameScene.getComponent<TileMapComponent>().tileWidth;c--){
if(c<0){c=0;}
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
boundaries[i] = c*gameScene.getComponent<TileMapComponent>().tileWidth+gameScene.getComponent<TileMapComponent>().tileWidth;
i++;
break;
}
}
}
}
/*
// ====== RIGHT ====
if (player.getComponent<TransformComponent>().velocity.x>0){
// ====== For Each Row ====
int i = 0;
for (int r=Game::pTileY-1;r<=Game::pTileY+1;r++){
if(r<0){r=0;}
// ====== For Each Tile (Column) =====
for (int c=Game::pTileX;c<Game::pTileX+Game::camera.w/gameScene.getComponent<TileMapComponent>().tileWidth;c++){
if(c<0){c=0;}
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
boundaries[i] = c*gameScene.getComponent<TileMapComponent>().tileWidth;
i++;
break;
}
}
}
}
// ===== UP ====
if (player.getComponent<TransformComponent>().velocity.y<0){
// ====== For Each Column ====
int i = 0;
for (int c=Game::pTileX-1;c<=Game::pTileX+1;c++){
if(c<0){c=0;}
// ====== For Each Tile (Row) =====
for (int r=Game::pTileY;r>Game::pTileY-Game::camera.h/gameScene.getComponent<TileMapComponent>().tileWidth;r--){
if(r<0){r=0;}
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
boundaries[i] = r*gameScene.getComponent<TileMapComponent>().tileWidth+gameScene.getComponent<TileMapComponent>().tileWidth;
i++;
break;
}
}
}
}
// ===== DOWN ====
if (player.getComponent<TransformComponent>().velocity.y>0){
// ====== For Each Column ====
int i = 0;
for (int c=Game::pTileX-1;c<=Game::pTileX+1;c++){
if(c<0){c=0;}
// ====== For Each Tile (Row) =====
for (int r=Game::pTileY;r<Game::pTileY+Game::camera.h/gameScene.getComponent<TileMapComponent>().tileWidth;r++){
if(r<0){r=0;}
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
boundaries[i] = r*gameScene.getComponent<TileMapComponent>().tileWidth+gameScene.getComponent<TileMapComponent>().tileWidth;
i++;
break;
}
}
}
}
*/
return boundaries;
}

View File

@ -7,15 +7,16 @@
#ifndef GAME_HPP_
#define GAME_HPP_
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_mixer.h"
#include "../../libsdl2/include/SDL.h"
#include "../../libsdl2_image/SDL_image.h"
#include "../../libsdl2_mixer/SDL_mixer.h"
#include <stdio.h>
#include <iostream>
#include <vector>
#include "Vector2D.h"
#include "../assetmgr/AssetManager.h"
#include "GameStateManager.h"
#include "../../libtmx-parser/src/tmxparser.h"
class ColliderComponent;
class AssetManager;
@ -29,20 +30,32 @@ public:
void update();
void render();
void clean();
void printDebug(char* debugInfo);
// void printDebug(std::string debugInfo);
static void drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue);
static int * predictCollisions();
bool running() { return isRunning; }
// static void AddTile(int srcX, int srcY, int xpos, int ypos);
static SDL_Renderer *renderer;
static SDL_Event event;
// static std::vector<ColliderComponent*> colliders;
// static tmxparser::TmxMap map;
// gameScene;
static bool debugMenu;
static bool isRunning;
static bool debugCollisionBoxes;
static bool gravityOnPlayer;
static bool playerIsGrounded;
static bool playerIsJumping;
static Vector2D playerPosition;
static Vector2D pVel;
static SDL_Rect camera;
static SDL_Rect levelMap;
static AssetManager* assets;
static GameStateManager* gsm;
static int pTileX;
static int pTileY;
std::string BoolToString(bool b);
enum groupLabels : std::size_t
{
groupMap,
@ -53,7 +66,9 @@ public:
groupObjects,
groupBackground,
groupUI_Layer0,
groupUI_Layer1
groupUI_Layer1,
groupUI_Layer2,
groupUI_Layer3
};
private:
int counter = 0;

View File

@ -19,7 +19,7 @@ int main(int argc, const char * argv[])
const int FPS = 60;
const int frameDelay = 1000 / FPS;
Uint32 frameStart;
Uint64 frameStart;
int frameTime;
// =============================
@ -30,7 +30,6 @@ int main(int argc, const char * argv[])
std::ifstream fin(configPath);
if(fin.is_open()){
// std::cout<<"config.json is opened successfully"<<std::endl;
std::ifstream jsonText("src/config/config.json");
std::ostringstream tmp;
tmp << jsonText.rdbuf();
@ -59,13 +58,13 @@ int main(int argc, const char * argv[])
while (game->running())
{
frameStart = SDL_GetTicks();
frameStart = SDL_GetTicks64();
game->handleEvents();
game->update();
game->render();
frameTime = SDL_GetTicks() - frameStart;
frameTime = SDL_GetTicks64() - frameStart;
if(frameDelay > frameTime)
{
@ -80,9 +79,7 @@ int main(int argc, const char * argv[])
if(fin.fail()){
std::cout<<"config.json load failed"<<std::endl;
} else{
// std::cout<<"config.json loaded"<<std::endl;
}
}
return 0;
}

View File

@ -74,11 +74,3 @@ void StateMachine::StateEngine(void)
}
}
}
// unsigned char getCurrentState()
// {
// const StateStruct* pStateMap = GetStateMap();
// this->*pStateMap[currentState];
// unsigned char state = StateMachine.currentState;
// return state;
// }

View File

@ -25,7 +25,7 @@ UINineSlice::~UINineSlice()
}
void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, int x1, int y0, int y1, SDL_Rect finalRect,int scale)
void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, int x1, int y0, int y1, SDL_Rect finalRect,int scale, Game::groupLabels group)
{
// Note about the variables: The source texture is what everything is drawn from, with srcW and srcH defining the size of that texture and x0 x1 being the vertical slices position across the x axis, and the y0, y1 being the horizontal slices. This is also in the project readme with an ASCII art diagram. finalRect refers to the Rectangle that describes the area of the screen in which we want the 9-sliced source to appear in it's final in-game rendering.
@ -49,20 +49,6 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
finalRect.w = finalRect.w*scale;
finalRect.h = finalRect.h*scale;
// x0 = x0*scale;
// x1 = x1*scale;
// y0 = y0*scale;
// y1 = y1*scale;
// printf("finalRect x:%d, y:%d, w:%d, h:%d \n",finalRect.x,finalRect.y,finalRect.w,finalRect.h);
// destRect.x = destRect.x*scale;
// destRect.y = destRect.y*scale;
// destRect.w = destRect.w*scale;
// destRect.h = destRect.h*scale;
// colsRemainder = colsRemainder*scale;
// rowsRemainder = rowsRemainder*scale;
for (int i=0; i<9; i++)
{
switch(i)
@ -72,7 +58,6 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
srcRect.y = 0;
srcRect.w = x0;
srcRect.h = y0;
// printf("tile zero.w or x0: %d\n",x0);
destRect.w = srcRect.w;
destRect.h = srcRect.h;
break;
@ -91,8 +76,6 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
srcRect.h = y0;
destRect.w = srcRect.w;
destRect.h = srcRect.h;
// printf("srcRect.x: %d",srcRect.x);
// printf("UI9Slice #2 srcRect x:%d y:%d w:%d h:%d \n",srcRect.x,srcRect.y,srcRect.w,srcRect.h);
break;
case 3:
srcRect.x = 0;
@ -150,14 +133,6 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
destRect.w = 0;
destRect.h = 0;
}
// These will be the same for each SDL_Rect except if there's a scaling int, which still needs to be implemented
// destRect.w = srcRect.w*scale;
// destRect.h = srcRect.h*scale;
// x0 = x0*scale;
// x1 = x1*scale;
// y0 = y0*scale;
// y1 = y1*scale;
// Calculate where and how many tiles to place
// We only need one instance of each of these in each corner or slices 0,2,6,8
@ -183,10 +158,7 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
destRect.x = finalRect.x+(finalRect.w-srcRect.w*scale);
destRect.y = finalRect.y+(finalRect.h-srcRect.h*scale);
}
AddSlice(srcRect,destRect,scale);
// printf("Corner Slice\n");
// printf("srcRect x:%d, y:%d, w:%d, h:%d \n",srcRect.x,srcRect.y,srcRect.w,srcRect.h);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n\n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
// Slices 1,7 need to be repeated in a row
if (i==1||i==7)
@ -197,20 +169,14 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
{
destRect.x = finalRect.x+(x0*scale+c*(x1*scale-x0*scale));
destRect.y = finalRect.y;
AddSlice(srcRect,destRect,scale);
// printf("Top Slice (1) \n");
// printf("srcRect x:%d, y:%d, w:%d, h:%d \n",srcRect.x,srcRect.y,srcRect.w,srcRect.h);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n\n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
if (colsRemainder>0){
destRect.x = finalRect.x+(x0*scale+cols*(x1*scale-x0*scale));
destRect.y = finalRect.y;
srcRect.w = colsRemainder;
destRect.w = colsRemainder;
AddSlice(srcRect,destRect,scale);
// printf("Top Slice (1) \n");
// printf("srcRect x:%d, y:%d, w:%d, h:%d \n",srcRect.x,srcRect.y,srcRect.w,srcRect.h);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n\n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
}
if (i==7)
@ -219,16 +185,14 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
{
destRect.x = finalRect.x+(x0*scale+c*(x1*scale-x0*scale));
destRect.y = finalRect.y+(finalRect.h-(srcH-y1)*scale);
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
if (colsRemainder>0){
destRect.x = finalRect.x+(x0*scale+cols*(x1*scale-x0*scale));
destRect.y = finalRect.y+(finalRect.h-(srcH-y1)*scale);
srcRect.w = colsRemainder;
destRect.w = colsRemainder;
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
}
}
@ -241,16 +205,14 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
{
destRect.x = finalRect.x;
destRect.y = finalRect.y+(y0*scale+r*(y1-y0)*scale);
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
if (rowsRemainder>0){
destRect.x = finalRect.x;
destRect.y = finalRect.y+(y0*scale+rows*(y1-y0)*scale);
srcRect.h = rowsRemainder;
destRect.h = rowsRemainder;
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
}
if (i==5)
@ -259,16 +221,14 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
{
destRect.x = finalRect.x+(finalRect.w-(srcW-x1)*scale);
destRect.y = finalRect.y+(y0*scale+r*(y1-y0)*scale);
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
if (rowsRemainder>0){
destRect.x = finalRect.x+(finalRect.w-(srcW-x1)*scale);
destRect.y = finalRect.y+(y0*scale+rows*(y1-y0)*scale);
srcRect.h = rowsRemainder;
destRect.h = rowsRemainder;
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
}
}
@ -283,8 +243,7 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
{
destRect.x = finalRect.x+(x0*scale+c*(x1-x0)*scale);
destRect.y = finalRect.y+(y0*scale+rowY*scale);
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
if (colsRemainder>0)
{
@ -292,8 +251,7 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
destRect.y = finalRect.y+(y0*scale+rowY*scale);
srcRect.w = colsRemainder;
destRect.w = colsRemainder;
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
}
if (rowsRemainder>0)
@ -306,8 +264,7 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
destRect.h = rowsRemainder;
srcRect.w = (srcW-x0-(srcW-x1));
destRect.w = (srcW-x0-(srcW-x1));
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
}
if(rowsRemainder>0&&colsRemainder>0)
@ -318,16 +275,14 @@ void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, in
srcRect.h = rowsRemainder;
destRect.w = colsRemainder;
destRect.h = rowsRemainder;
AddSlice(srcRect,destRect,scale);
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
AddSlice(srcRect,destRect,scale,group);
}
}
}
}
// Need to take in variables: (srcRect, desiredRect)
void UINineSlice::AddSlice(SDL_Rect srcRect, SDL_Rect destRect, int scale)
void UINineSlice::AddSlice(SDL_Rect srcRect, SDL_Rect destRect, int scale, Game::groupLabels group)
{
// printf("adding a 9slice element");
auto& slice(manager.addEntity());
SDL_Rect scaledRect = SDL_Rect();
scaledRect.x = destRect.x;
@ -336,5 +291,5 @@ void UINineSlice::AddSlice(SDL_Rect srcRect, SDL_Rect destRect, int scale)
scaledRect.h = destRect.h*scale;
slice.addComponent<TransformComponent>(scaledRect.x, scaledRect.y, scaledRect.w, scaledRect.h, 1);
slice.addComponent<SpriteComponent>("textBox",SpriteComponent::spriteUIL0,srcRect,scaledRect);
slice.addGroup(Game::groupUI_Layer0);
slice.addGroup(group);
}

View File

@ -10,6 +10,7 @@
#include "SDL2/SDL.h"
#include "string"
#include "../game/Game.hpp"
class UINineSlice
{
@ -18,8 +19,8 @@ public:
SDL_Rect destRect;
UINineSlice(std::string texID);
~UINineSlice();
void MakeSlices(std::string texture, int srcW, int srcH, int x0, int x1, int y0, int y1, SDL_Rect destRect, int scale);
void AddSlice(SDL_Rect srcRect, SDL_Rect destRect, int scale);
void MakeSlices(std::string texture, int srcW, int srcH, int x0, int x1, int y0, int y1, SDL_Rect destRect, int scale, Game::groupLabels label);
void AddSlice(SDL_Rect srcRect, SDL_Rect destRect, int scale, Game::groupLabels label);
private:
};

View File

@ -16,7 +16,7 @@
extern Manager manager;
UIText::UIText(std::string text, std::string texId, int x, int y, int letterW, int letterH, int lScale)
UIText::UIText(std::string text, std::string texId, int x, int y, int letterW, int letterH, int lScale, std::string tag, Game::groupLabels group)
{
inputText = text;
textureID = texId;
@ -25,34 +25,30 @@ UIText::UIText(std::string text, std::string texId, int x, int y, int letterW, i
letterWidth = letterW;
letterHeight = letterH;
scale = lScale;
// gameGroup = Game::groupLabels::groupUI_Layer3;
auto& uiLetters(manager.addEntity());
uiLetters.setTag(tag);
uiLetters.addGroup(group);
}
UIText::~UIText()
{
}
void UIText::ParseString(std::string inputText, int x, int y, int scale, std::string tag)
void UIText::ParseString(std::string inputText, int x, int y, int letterScale, std::string tag, Game::groupLabels group)
{
// gameGroup = group;
//Parse input text into an array of char
int posX = x;
int posY = y;
int i = 0;
// printf(inputText);
char current = inputText[i];
// const char* newLineChar{10};
int charsNumber = inputText.length();
// printf("Counting string \'%s\': \n",inputText.c_str());
// printf("%d\n",charsNumber);
// std::vector<char> writableStr(inputText.begin(), inputText.end());
// writableStr.push_back('\0');
do
{
// for (int i = 0; i < writableStr.size(); i++)
// {
// if (writableStr.at(i) == '\n')
// {
// printf("found new line");
// }
// }
++i;
if (strcmp(&current,"\n")!=0)
{
@ -60,26 +56,36 @@ void UIText::ParseString(std::string inputText, int x, int y, int scale, std::st
}
else
{
// printf("new line detected/n");
posX = x;
posY += letterHeight;
}
UIText::AddLetter(posX, posY, current, tag);
UIText::AddLetter(posX, posY, current, tag, letterScale, group);
current = inputText[i];
} while ((strcmp(&current,"\0"))!=0);
}
void UIText::AddLetter(int xpos, int ypos, char crnt, std::string tag)
void UIText::AddLetter(int xpos, int ypos, char crnt, std::string tag, int lttrScale, Game::groupLabels groupLabel)
{
auto& letter(manager.addEntity());
letter.addComponent<TransformComponent>(xpos*scale, ypos*scale, letterWidth, letterHeight, 1);
// printf("Scale: %d\n",scale);
letter.addComponent<SpriteComponent>("font", SpriteComponent::spriteText, crnt, letterWidth, letterHeight, scale);
letter.setTag(tag);
letter.addGroup(Game::groupUI_Layer1);
// =======THIS NEEDS TO BE REFACTORED TO NOT USE INDIVIDUAL ENTITIES FOR EACH LETTER============
// auto& letter(manager.addEntity());
// letter.addComponent<TransformComponent>(xpos*lttrScale, ypos*lttrScale, letterWidth, letterHeight, 1);
// letter.addComponent<SpriteComponent>("font", SpriteComponent::spriteText, crnt, letterWidth, letterHeight, lttrScale);
// letter.setTag(tag);
// letter.addGroup(groupLabel);
SDL_Texture* letterTexture;
letterTexture = Game::assets->GetTexture(textureID);
SDL_Rect srcRect,destRect;
srcRect.x = ((crnt-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth;
srcRect.y = ((crnt-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight;
srcRect.w = letterWidth;
srcRect.h = letterHeight;
destRect.x = xpos;
destRect.y = ypos;
destRect.w = letterWidth*scale;
destRect.h = letterHeight*scale;
TextureManager::Draw(letterTexture,srcRect,destRect,SDL_FLIP_NONE);
}
// void UIText::RemoveLetter()
// {
// }

View File

@ -8,25 +8,47 @@
#ifndef SRC_UITEXT_H_
#define SRC_UITEXT_H_
#define ASCII_START_IDX 32
#define ASCII_COUNT 96
#define ASCII_ROW_COUNT 16
#include "SDL2/SDL.h"
#include <iostream>
#include "../game/Game.hpp"
#include "../assetmgr/TextureManager.h"
#include "../assetmgr/AssetManager.h"
class UIText
{
private:
// SDL_Texture *texture;
// SDL_Rect srcRect, destRect;
char letter;
// int frames = 0;
// int speed = 100;
int letterWidth, letterHeight;
int scale = 1;
public:
std::string inputText;
int letterHeight;
int letterWidth;
// int letterHeight;
// int letterWidth;
// virtual void init() {}
// virtual void update() {}
// virtual void draw() {}
Game::groupLabels gameGroup;
int posX;
int posY;
std::string textureID;
UIText(std::string inputText, std::string texID, int x, int y, int letterW, int letterH, int lScale);
UIText(std::string inputText, std::string texID, int x, int y, int letterW, int letterH, int lScale, std::string tag, Game::groupLabels group);
~UIText();
// void SetCharClips(SDL_Texture* fontTex, int x, int y, int letterW, int letterH);
void AddLetter(int xpos, int ypos, char crnt, std::string tag);
void ParseString(std::string inputText, int x, int y, int scale, std::string tag);
int scale;
void init() {}
void update() {}
void draw() {}
void AddLetter(int xpos, int ypos, char crnt, std::string tag, int lttrScale, Game::groupLabels groupLabel);
void ParseString(std::string inputText, int x, int y, int letterScale, std::string tag, Game::groupLabels group);
// void setTex(std::string id);
// int scale;
};
#endif /* SRC_UITEXT_H_ */