commit 2b46608d68360fdaad8862cfefacb2d095fd6698 Author: Kayne Ruse Date: Tue Jun 17 01:40:06 2014 +1000 Initial commit, basic infrastructure diff --git a/makefile b/makefile new file mode 100644 index 0000000..6987ff8 --- /dev/null +++ b/makefile @@ -0,0 +1,22 @@ +#for use on Windows: + +#MKDIR=mkdir +#RM=del /y + +CXXFLAGS+=-static-libgcc -static-libstdc++ + +export + +OUTDIR=out + +all: $(OUTDIR) + $(MAKE) -C perlin + $(MAKE) -C src + +$(OUTDIR): + mkdir $(OUTDIR) + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/perlin/makefile b/perlin/makefile new file mode 100644 index 0000000..a4efd0c --- /dev/null +++ b/perlin/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/perlin/perlin_noise.cpp b/perlin/perlin_noise.cpp new file mode 100644 index 0000000..1cc9c6c --- /dev/null +++ b/perlin/perlin_noise.cpp @@ -0,0 +1,91 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "perlin_noise.hpp" + +#include "vector2.hpp" + +#include + +//------------------------- +//Public access members +//------------------------- + +double SimpleRNG::operator()(double x) { + x = x * seed * 345589144.0 + 1123581321.0; + return fmod(x, 21795.0); +}; + +Vector2 PerlinNoise::Gradient(Vector2 pos) { + //could cache this result + double j = rng(pos.y*pos.y + pos.x * 144); + double i = rng(pos.x*pos.x + pos.y * 144); + Vector2 v(i, j); + v.Normalize(); + return v; +} + +double PerlinNoise::Influcence(Vector2 gridPoint, Vector2 queryPoint) { + return ScalarProduct(Gradient(gridPoint), queryPoint - gridPoint); +} + +double PerlinNoise::Noise(double x, double y, double width, double height) { + Vector2 point = {x, y}; + + x = Snap(x, width); + y = Snap(y, height); + + //determine the grid points + Vector2 tl = {x, y}; + Vector2 tr = {x + width, y}; + Vector2 bl = {x, y + height}; + Vector2 br = {x + width, y + height}; + + //influence equasion + double s = Influcence(tl, point); + double t = Influcence(tr, point); + double u = Influcence(bl, point); + double v = Influcence(br, point); + + //get the noise + double a = s + Curve((t - s) / width); + double b = u + Curve((v - u) / width); + return a + Curve((b - a) / height); +} + +//------------------------- +//Static utility functions +//------------------------- + +double PerlinNoise::Curve(double x) { + //param: 0 to 1 inclusive + return 3.0 * pow(x, 2.0) - 2.0 * pow(x, 3.0); +} + +double PerlinNoise::Snap(double x, double base) { + //snap to a grid (floating point version) + return floor(x / base) * base; +} + +double PerlinNoise::ScalarProduct(Vector2 lhs, Vector2 rhs) { + //the dot product + return lhs.x * rhs.x + lhs.y * rhs.y; +} \ No newline at end of file diff --git a/perlin/perlin_noise.hpp b/perlin/perlin_noise.hpp new file mode 100644 index 0000000..1705fd1 --- /dev/null +++ b/perlin/perlin_noise.hpp @@ -0,0 +1,58 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef PERLINNOISE_HPP_ +#define PERLINNOISE_HPP_ + +#include "vector2.hpp" + +class SimpleRNG { +public: + SimpleRNG() = default; + SimpleRNG(double x) { SetSeed(x); } + + double SetSeed(double s) { return seed = s; } + double GetSeed() { return seed; } + + double operator()(double x); + +private: + double seed = 8891.0; +}; + +class PerlinNoise { +public: + PerlinNoise() = default; + ~PerlinNoise() = default; + + Vector2 Gradient(Vector2); + double Influcence(Vector2 gridPoint, Vector2 queryPoint); + + double Noise(double x, double y, double width, double height); +private: + static inline double Curve(double x); + static inline double Snap(double x, double base); + static inline double ScalarProduct(Vector2 lhs, Vector2 rhs); + + SimpleRNG rng; +}; + +#endif diff --git a/perlin/vector2.hpp b/perlin/vector2.hpp new file mode 100644 index 0000000..2be3d34 --- /dev/null +++ b/perlin/vector2.hpp @@ -0,0 +1,108 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef VECTOR2_HPP_ +#define VECTOR2_HPP_ + +#include +#include +#include + +class Vector2 { +public: + double x, y; + + Vector2() = default; + Vector2(double i, double j): x(i), y(j) {}; + ~Vector2() = default; + Vector2& operator=(Vector2 const&) = default; + + double Length() const { + return sqrt(x*x+y*y); + } + double SquaredLength() const { + return x*x+y*y; + } + void Normalize() { + double l = Length(); + x /= l; + y /= l; + } + + //Arithmetic operators + Vector2 operator+(Vector2 v) const { + Vector2 ret; + ret.x = x + v.x; + ret.y = y + v.y; + return ret; + } + Vector2 operator-(Vector2 v) const { + Vector2 ret; + ret.x = x - v.x; + ret.y = y - v.y; + return ret; + } + Vector2 operator*(Vector2 v) const { + Vector2 ret; + ret.x = x * v.x; + ret.y = y * v.y; + return ret; + } + Vector2 operator*(double d) const { + Vector2 ret; + ret.x = x * d; + ret.y = y * d; + return ret; + } + + Vector2 operator/(Vector2 v) { + if (!v.x || !v.y) + throw(std::domain_error("Divide by zero")); + Vector2 ret; + ret.x = x / v.x; + ret.y = y / v.y; + return ret; + } + Vector2 operator/(double d) { + if (!d) + throw(std::domain_error("Divide by zero")); + Vector2 ret; + ret.x = x / d; + ret.y = y / d; + return ret; + } + + bool operator==(Vector2 v) { return (x == v.x && y == v.y); } + bool operator!=(Vector2 v) { return (x != v.x || y != v.y); } + + //member templates (curry the above operators) + template Vector2 operator+=(T t) { return *this = *this + t; } + template Vector2 operator-=(T t) { return *this = *this - t; } + template Vector2 operator*=(T t) { return *this = *this * t; } + template Vector2 operator/=(T t) { return *this = *this / t; } + template bool operator==(T t) { return (x == t && y == t); } + template bool operator!=(T t) { return (x != t || y != t); } +}; + +//This is explicitly a POD +static_assert(std::is_pod::value, "Vector2 is not a POD"); + +#endif diff --git a/src/application.cpp b/src/application.cpp new file mode 100644 index 0000000..3622bcc --- /dev/null +++ b/src/application.cpp @@ -0,0 +1,110 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial Applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "application.hpp" + +#include +#include +#include + +//------------------------- +//Scene headers +//------------------------- + +//Add the custom scene headers here +#include "shell_scene.hpp" + +//------------------------- +//Public access members +//------------------------- + +void Application::Init(int argc, char** argv) { + //initialize SDL + if (SDL_Init(SDL_INIT_VIDEO)) { + throw(std::runtime_error("Failed to initialize SDL")); + } + BaseScene::SetScreen(800, 600, 0, SDL_HWSURFACE|SDL_DOUBLEBUF); +} + +void Application::Proc() { + LoadScene(SceneList::FIRST); + + //prepare the time system + typedef std::chrono::steady_clock Clock; + + std::chrono::duration delta(16); + Clock::time_point simTime = Clock::now(); + Clock::time_point realTime; + + //The main loop + while(activeScene->GetNextScene() != SceneList::QUIT) { + //switch scenes when necessary + if (activeScene->GetNextScene() != SceneList::CONTINUE) { + LoadScene(activeScene->GetNextScene()); + continue; + } + + //update the current time + realTime = Clock::now(); + + //simulate game time + while (simTime < realTime) { + //call each user defined function + activeScene->RunFrame(double(delta.count()) / std::chrono::duration::period::den); + simTime += delta; + } + + //draw the game to the screen + activeScene->RenderFrame(); + } + + UnloadScene(); +} + +void Application::Quit() { + SDL_Quit(); +} + +//------------------------- +//Private access members +//------------------------- + +void Application::LoadScene(SceneList sceneIndex) { + UnloadScene(); + switch(sceneIndex) { + //add scene creation calls here + case SceneList::FIRST: + case SceneList::SHELL: + activeScene = new ShellScene(); + break; + default: { + std::ostringstream msg; + msg << "Failed to recognize the scene index: "; + msg << sceneIndex; + throw(std::logic_error(msg.str())); + } + } +} + +void Application::UnloadScene() { + delete activeScene; + activeScene = nullptr; +} diff --git a/src/application.hpp b/src/application.hpp new file mode 100644 index 0000000..63b7ec7 --- /dev/null +++ b/src/application.hpp @@ -0,0 +1,45 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef APPLICATION_HPP_ +#define APPLICATION_HPP_ + +#include "scene_list.hpp" +#include "base_scene.hpp" + +class Application { +public: + Application() = default; + ~Application() = default; + + void Init(int argc, char** argv); + void Proc(); + void Quit(); + +private: + //Private access members + void LoadScene(SceneList sceneIndex); + void UnloadScene(); + + BaseScene* activeScene = nullptr; +}; + +#endif diff --git a/src/base_scene.cpp b/src/base_scene.cpp new file mode 100644 index 0000000..48512ec --- /dev/null +++ b/src/base_scene.cpp @@ -0,0 +1,139 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "base_scene.hpp" + +#include + +//------------------------- +//Static declarations +//------------------------- + +SDL_Surface* BaseScene::screen = nullptr; + +//------------------------- +//Public access members +//------------------------- + +BaseScene::BaseScene() { + // +} + +BaseScene::~BaseScene() { + // +} + +//------------------------- +//Program control +//------------------------- + +SDL_Surface* BaseScene::SetScreen(int w, int h, int bpp, Uint32 flags) { + if (!bpp) { + bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel; + } + + screen = SDL_SetVideoMode(w, h, bpp, flags); + + if (!screen) { + throw(std::runtime_error("Failed to create the screen surface")); + } + + return screen; +} + +SDL_Surface* BaseScene::GetScreen() { + return screen; +} + +SceneList BaseScene::SetNextScene(SceneList sceneIndex) { + return nextScene = sceneIndex; +} + +SceneList BaseScene::GetNextScene() const { + return nextScene; +} + +//------------------------- +//Frame loop +//------------------------- + +void BaseScene::RunFrame(double delta) { + FrameStart(); + HandleEvents(); + Update(delta); + FrameEnd(); +} + +void BaseScene::RenderFrame() { + SDL_FillRect(screen, 0, 0); + Render(screen); + SDL_Flip(screen); +} + +//------------------------- +//Event handlers +//------------------------- + +void BaseScene::HandleEvents() { + SDL_Event event; + + while(SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_QUIT: + QuitEvent(); + break; + + case SDL_VIDEORESIZE: + SetScreen(event.resize.w, event.resize.h, 0, screen->flags); + break; + + case SDL_MOUSEMOTION: + MouseMotion(event.motion); + break; + + case SDL_MOUSEBUTTONDOWN: + MouseButtonDown(event.button); + break; + + case SDL_MOUSEBUTTONUP: + MouseButtonUp(event.button); + break; + + case SDL_KEYDOWN: + KeyDown(event.key); + break; + + case SDL_KEYUP: + KeyUp(event.key); + break; + +#ifdef USE_EVENT_JOYSTICK + //TODO: joystick/gamepad support +#endif + +#ifdef USE_EVENT_UNKNOWN + default: + UnknownEvent(event); + break; +#endif + }//switch + }//while +} diff --git a/src/base_scene.hpp b/src/base_scene.hpp new file mode 100644 index 0000000..a4bfd88 --- /dev/null +++ b/src/base_scene.hpp @@ -0,0 +1,74 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef BASESCENE_HPP_ +#define BASESCENE_HPP_ + +#include "scene_list.hpp" + +#include "SDL/SDL.h" + +class BaseScene { +public: + //Public access members + BaseScene(); + virtual ~BaseScene(); + + //Program control + static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF); + static SDL_Surface* GetScreen(); + + SceneList SetNextScene(SceneList sceneIndex); + SceneList GetNextScene() const; + + //Frame loop + virtual void RunFrame(double delta); + virtual void RenderFrame(); + +protected: + virtual void FrameStart() {} + virtual void HandleEvents(); + virtual void Update(double delta) {} + virtual void FrameEnd() {} + virtual void Render(SDL_Surface* const screen) {} + + //Event handlers + virtual void QuitEvent() { SetNextScene(SceneList::QUIT); } + virtual void MouseMotion(SDL_MouseMotionEvent const&) {} + virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {} + virtual void MouseButtonUp(SDL_MouseButtonEvent const&) {} + virtual void KeyDown(SDL_KeyboardEvent const&) {} + virtual void KeyUp(SDL_KeyboardEvent const&) {} + +#ifdef USE_EVENT_JOYSTICK + //TODO: joystick/gamepad support +#endif + +#ifdef USE_EVENT_UNKNOWN + virtual void UnknownEvent(SDL_Event const&) {} +#endif + +private: + static SDL_Surface* screen; + SceneList nextScene = SceneList::CONTINUE; +}; + +#endif diff --git a/src/image.cpp b/src/image.cpp new file mode 100644 index 0000000..3b4f99b --- /dev/null +++ b/src/image.cpp @@ -0,0 +1,145 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "image.hpp" + +#include +#include + +Image& Image::operator=(Image const& rhs) { + //don't screw yourself + if (this == &rhs) { + return *this; + } + + FreeSurface(); + + //Copy the other Image's stuff + surface = rhs.surface; + clip = rhs.clip; + local = false; +} + +Image& Image::operator=(Image&& rhs) { + //don't screw yourself + if (this == &rhs) { + return *this; + } + + FreeSurface(); + + //Steal the other Image's stuff + surface = rhs.surface; + clip = rhs.clip; + local = rhs.local; + + rhs.surface = nullptr; + rhs.clip = {0, 0, 0, 0}; + rhs.local = false; +} + +SDL_Surface* Image::LoadSurface(std::string fname) { + FreeSurface(); + SDL_Surface* p = SDL_LoadBMP(fname.c_str()); + if (!p) { + std::ostringstream os; + os << "Failed to load file: " << fname; + throw(std::runtime_error(os.str())); + } + surface = p; + clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; + local = true; + SetTransparentColor(255, 0, 255); //default + return surface; +} + +SDL_Surface* Image::CreateSurface(Uint16 w, Uint16 h) { + FreeSurface(); + Uint32 rmask, gmask, bmask, amask; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; +#else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; +#endif + SDL_Surface* p = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, rmask, gmask, bmask, amask); + if (!p) { + throw(std::runtime_error("Failed to create Image surface")); + } + surface = p; + clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; + local = true; + SetTransparentColor(255, 0, 255); //default + return surface; +} + +SDL_Surface* Image::SetSurface(SDL_Surface* p) { + FreeSurface(); + if (!p) { + throw(std::invalid_argument("No surface pointer provided")); + } + surface = p; + clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; + local = false; + return surface; +} + +void Image::FreeSurface() { + if (local) { + SDL_FreeSurface(surface); + local = false; + } + surface = nullptr; + clip = {0, 0, 0, 0}; +} + +void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) { + if (!surface) { + throw(std::logic_error("No image surface to draw")); + } + SDL_Rect sclip = clip, dclip = {x,y}; + SDL_BlitSurface(surface, &sclip, dest, &dclip); +} + +void Image::SetTransparentColor(Uint8 r, Uint8 g, Uint8 b) { + if (!surface) { + throw(std::logic_error("Failed to set the transparent color")); + } + if (!local) { + throw(std::logic_error("Cannot set the transparent color of a non-local surface")); + } + SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, r, g, b)); +} + +void Image::ClearTransparentColor() { + if (!surface) { + throw(std::logic_error("Failed to clear the transparent color")); + } + if (!local) { + throw(std::logic_error("Cannot clear the transparent color of a non-local surface")); + } + SDL_SetColorKey(surface, 0, 0); +} diff --git a/src/image.hpp b/src/image.hpp new file mode 100644 index 0000000..462e565 --- /dev/null +++ b/src/image.hpp @@ -0,0 +1,73 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef IMAGE_HPP_ +#define IMAGE_HPP_ + +#include "SDL/SDL.h" +#include + +class Image { +public: + Image() = default; + Image(Image const& rhs) { *this = rhs; } + Image(Image&& rhs) { *this = std::move(rhs); } + Image(std::string fname) { LoadSurface(fname); } + Image(Uint16 w, Uint16 h) { CreateSurface(w, h); } + Image(SDL_Surface* p) { SetSurface(p); } + ~Image() { FreeSurface(); } + + Image& operator=(Image const&); + Image& operator=(Image&&); + + SDL_Surface* LoadSurface(std::string fname); + SDL_Surface* CreateSurface(Uint16 w, Uint16 h); + SDL_Surface* SetSurface(SDL_Surface*); + SDL_Surface* GetSurface() const { return surface; } + void FreeSurface(); + + void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); + + //Clip handlers + SDL_Rect SetClip(SDL_Rect r) { return clip = r; } + SDL_Rect GetClip() const { return clip; } + + Sint16 SetClipX(Sint16 x) { return clip.x = x; } + Sint16 SetClipY(Sint16 y) { return clip.y = y; } + Uint16 SetClipW(Uint16 w) { return clip.w = w; } + Uint16 SetClipH(Uint16 h) { return clip.h = h; } + + Sint16 GetClipX() const { return clip.x; } + Sint16 GetClipY() const { return clip.y; } + Uint16 GetClipW() const { return clip.w; } + Uint16 GetClipH() const { return clip.h; } + + bool GetLocal() const { return local; } + + void SetTransparentColor(Uint8 r, Uint8 g, Uint8 b); + void ClearTransparentColor(); +protected: + SDL_Surface* surface = nullptr; + SDL_Rect clip = {0, 0, 0, 0}; + bool local = false; +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..213bc79 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,43 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "application.hpp" + +#include +#include + +using namespace std; + +int main(int argc, char** argv) { + cout << "Beginning " << argv[0] << endl; + try { + Application app; + app.Init(argc, argv); + app.Proc(); + app.Quit(); + } + catch(exception& e) { + cerr << "Fatal exception thrown: " << e.what() << endl; + return 1; + } + cout << "Clean exit" << endl; + return 0; +} diff --git a/src/makefile b/src/makefile new file mode 100644 index 0000000..61e4359 --- /dev/null +++ b/src/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../perlin +LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=../out +OUT=$(addprefix $(OUTDIR)/,shell) + +#targets +all: $(OBJ) $(OUT) + $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/src/scene_list.hpp b/src/scene_list.hpp new file mode 100644 index 0000000..f0ceb01 --- /dev/null +++ b/src/scene_list.hpp @@ -0,0 +1,35 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef SCENELIST_HPP_ +#define SCENELIST_HPP_ + +enum SceneList { + //these are reserved + QUIT, + CONTINUE, + FIRST, + + //custom indexes + SHELL, +}; + +#endif diff --git a/src/shell_scene.cpp b/src/shell_scene.cpp new file mode 100644 index 0000000..fa8ad81 --- /dev/null +++ b/src/shell_scene.cpp @@ -0,0 +1,95 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "shell_scene.hpp" + +//------------------------- +//Public access members +//------------------------- + +void setPixel(SDL_Surface* const dest, int x, int y, int colour) { + *(static_cast(dest->pixels) + dest->w * y + x) = colour; +} + +ShellScene::ShellScene() { + //test the generator + int width = 800; + int height = 600; + image.CreateSurface(width, height); + + int white = SDL_MapRGB(image.GetSurface()->format, 255, 255, 255); + + double mod; + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + mod = generator.Noise(i, j, width, height); + setPixel(image.GetSurface(), i, j, white*mod); + } + } +} + +ShellScene::~ShellScene() { + // +} + +//------------------------- +//Frame loop +//------------------------- + +void ShellScene::FrameStart() { + // +} + +void ShellScene::Update(double delta) { + // +} + +void ShellScene::FrameEnd() { + // +} + +void ShellScene::Render(SDL_Surface* const screen) { + image.DrawTo(screen, 0, 0); +} + +//------------------------- +//Event handlers +//------------------------- + +void ShellScene::MouseMotion(SDL_MouseMotionEvent const& motion) { + // +} + +void ShellScene::MouseButtonDown(SDL_MouseButtonEvent const& button) { + // +} + +void ShellScene::MouseButtonUp(SDL_MouseButtonEvent const& button) { + // +} + +void ShellScene::KeyDown(SDL_KeyboardEvent const& key) { + // +} + +void ShellScene::KeyUp(SDL_KeyboardEvent const& key) { + // +} diff --git a/src/shell_scene.hpp b/src/shell_scene.hpp new file mode 100644 index 0000000..3c9588d --- /dev/null +++ b/src/shell_scene.hpp @@ -0,0 +1,55 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef SHELLSCENE_HPP_ +#define SHELLSCENE_HPP_ + +#include "base_scene.hpp" + +#include "image.hpp" +#include "perlin_noise.hpp" + +class ShellScene : public BaseScene { +public: + //Public access members + ShellScene(); + ~ShellScene(); + +protected: + //Frame loop + void FrameStart(); + void Update(double delta); + void FrameEnd(); + void Render(SDL_Surface* const); + + //Event handlers + void MouseMotion(SDL_MouseMotionEvent const&); + void MouseButtonDown(SDL_MouseButtonEvent const&); + void MouseButtonUp(SDL_MouseButtonEvent const&); + void KeyDown(SDL_KeyboardEvent const&); + void KeyUp(SDL_KeyboardEvent const&); + + //members + PerlinNoise generator; + Image image; +}; + +#endif