From 61c2d30a60d586cbe63885885c6a373c7713af1e Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 23 Dec 2013 14:59:45 +0100 Subject: [PATCH] move to SDL2 wow, this is great. yay --- build/config.mk | 7 +- build/space.mk | 2 - src/app/Application.cpp | 51 ++++--- src/app/Application.h | 10 +- src/app/SDL.cpp | 19 +++ src/app/SDL.h | 22 +++ src/entity/Entity.h | 2 +- src/graphics/Camera.cpp | 8 +- src/graphics/Camera.h | 7 +- src/graphics/Canvas.cpp | 227 +++++++++++++++++++++++++++++ src/graphics/Canvas.h | 57 ++++++++ src/graphics/Color.h | 12 +- src/graphics/Moveable.h | 2 +- src/{math => graphics}/Vector.h | 0 src/graphics/Window.cpp | 58 ++++++++ src/graphics/Window.h | 41 ++++++ src/graphics/primitive.cpp | 249 -------------------------------- src/graphics/primitive.h | 63 -------- src/sdl/InitSDL.cpp | 20 --- src/sdl/InitSDL.h | 22 --- src/sdl/InitScreen.cpp | 40 ----- src/sdl/InitScreen.h | 33 ----- src/space.cpp | 19 ++- src/world/Universe.h | 2 +- 24 files changed, 488 insertions(+), 485 deletions(-) create mode 100644 src/app/SDL.cpp create mode 100644 src/app/SDL.h create mode 100644 src/graphics/Canvas.cpp create mode 100644 src/graphics/Canvas.h rename src/{math => graphics}/Vector.h (100%) create mode 100644 src/graphics/Window.cpp create mode 100644 src/graphics/Window.h delete mode 100644 src/graphics/primitive.cpp delete mode 100644 src/graphics/primitive.h delete mode 100644 src/sdl/InitSDL.cpp delete mode 100644 src/sdl/InitSDL.h delete mode 100644 src/sdl/InitScreen.cpp delete mode 100644 src/sdl/InitScreen.h diff --git a/build/config.mk b/build/config.mk index efbf376..d5a8fab 100644 --- a/build/config.mk +++ b/build/config.mk @@ -17,11 +17,8 @@ LDFLAGS ?= CXXFLAGS += -Wall -Werror # libraries -SDL_FLAGS = $(shell pkg-config --cflags sdl) -SDL_LIBS = $(shell pkg-config --libs sdl) - -SDL_IMG_FLAGS = $(shell pkg-config --cflags SDL_image) -SDL_IMG_LIBS = $(shell pkg-config --libs SDL_image) +SDL_FLAGS = $(shell pkg-config --cflags sdl2) +SDL_LIBS = $(shell pkg-config --libs sdl2) CPPUNIT_FLAGS = $(shell pkg-config --cflags cppunit) CPPUNIT_LIBS = $(shell pkg-config --libs cppunit) diff --git a/build/space.mk b/build/space.mk index 8a6f147..446cf04 100644 --- a/build/space.mk +++ b/build/space.mk @@ -12,11 +12,9 @@ SPACE_TEST_EXES := test-all SPACE_FLAGS = $(sort $(strip \ $(SDL_FLAGS) \ - $(SDL_IMG_FLAGS) \ )) SPACE_LIBS = $(sort $(strip \ $(SDL_LIBS) \ - $(SDL_IMG_LIBS) \ )) SPACE_TEST_FLAGS = $(SPACE_FLAGS) $(CPPUNIT_FLAGS) diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 20c3718..488b952 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -1,17 +1,16 @@ #include "Application.h" +#include "../graphics/Canvas.h" #include "../graphics/Color.h" -#include "../graphics/primitive.h" -#include "../sdl/InitScreen.h" namespace space { -Application::Application(InitScreen &s) -: screen(s) +Application::Application(Canvas &c) +: canvas(c) , univ(Vector(10, 10), Vector(10, 10), Vector(10, 10)) , focus(Vector(500, 500), 500) -, cam(800, 800, focus.Pos()) +, cam(c.Size(), focus.Pos()) , last(SDL_GetTicks()) , running(false) { controlled = univ.AddEntity(Entity()); @@ -33,7 +32,7 @@ void Application::Loop(int delta) { HandleEvents(); Update(delta); Render(); - screen.Flip(); + canvas.Present(); } @@ -44,15 +43,20 @@ void Application::HandleEvents() { case SDL_QUIT: running = false; break; - case SDL_VIDEORESIZE: - screen.Resize(event.resize.w, event.resize.h); - cam.Resize(event.resize.w, event.resize.h); + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_RESIZED) { + cam.Resize(event.window.data1, event.window.data2); + } break; case SDL_KEYDOWN: - OnKeyDown(event.key); + if (!event.key.repeat) { + OnKeyDown(event.key); + } break; case SDL_KEYUP: - OnKeyUp(event.key); + if (!event.key.repeat) { + OnKeyUp(event.key); + } break; default: // skip event @@ -153,18 +157,25 @@ void Application::Render() { constexpr Color entityColor(0x00, 0xAA, 0xAA); constexpr Color focusColor(0xFA, 0xFA, 0x00); - SDL_Surface *dst = screen.Screen(); - const Vector begin = cam.ToScreen(Vector(0, 0)); - const Vector end = - cam.ToScreen((univ.size * univ.secSize * univ.areaSize)) - + Vector(1, 1); + canvas.SetColor(background); + canvas.Fill(); - Fill(dst, background); - Grid2(dst, begin, end, cam.ToScale(univ.areaSize), univ.secSize, secGrid, univGrid); - Cross(dst, cam.ToScreen(focus.Pos()), 15, focusColor); + canvas.Grid2( + cam.ToScreen(Vector(0, 0)), + cam.ToScale(univ.size * univ.secSize * univ.areaSize), + cam.ToScale(univ.areaSize), + univ.secSize, + secGrid, + univGrid); + canvas.SetColor(focusColor); + canvas.Cross(cam.ToScreen(focus.Pos()), 15); + + canvas.SetColor(entityColor); for (const Entity &e : univ.Entities()) { - Cross(dst, cam.ToScreen((e.area * univ.areaSize) + Vector(e.pos)), 10, entityColor); + canvas.Cross( + cam.ToScreen(Vector(e.area * univ.areaSize) + e.pos), + 10); } } diff --git a/src/app/Application.h b/src/app/Application.h index 61c3f45..b5f0a6b 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -3,21 +3,21 @@ #include "../graphics/Camera.h" #include "../graphics/Moveable.h" -#include "../math/Vector.h" +#include "../graphics/Vector.h" #include "../world/Universe.h" -#include +#include namespace space { -class InitScreen; +class Canvas; class Entity; class Application { public: - explicit Application(InitScreen &); + explicit Application(Canvas &); public: void Run(); @@ -34,7 +34,7 @@ private: void Render(); private: - InitScreen &screen; + Canvas &canvas; Universe univ; diff --git a/src/app/SDL.cpp b/src/app/SDL.cpp new file mode 100644 index 0000000..e5b48ca --- /dev/null +++ b/src/app/SDL.cpp @@ -0,0 +1,19 @@ +#include "SDL.h" + +#include +#include + + +namespace space { + +SDL::SDL(Uint32 flags) { + if (SDL_Init(flags) != 0) { + throw std::runtime_error(std::string("init SDL: ") + SDL_GetError()); + } +} + +SDL::~SDL() { + SDL_Quit(); +} + +} diff --git a/src/app/SDL.h b/src/app/SDL.h new file mode 100644 index 0000000..ea2a198 --- /dev/null +++ b/src/app/SDL.h @@ -0,0 +1,22 @@ +#ifndef SPACE_SDL_H_ +#define SPACE_SDL_H_ + +#include + + +namespace space { + +class SDL { + +public: + explicit SDL(Uint32 flags); + ~SDL(); + + SDL(const SDL &) = delete; + SDL &operator =(const SDL &) = delete; + +}; + +} + +#endif diff --git a/src/entity/Entity.h b/src/entity/Entity.h index 9fd2dff..eb95331 100644 --- a/src/entity/Entity.h +++ b/src/entity/Entity.h @@ -1,7 +1,7 @@ #ifndef SPACE_ENTITY_H_ #define SPACE_ENTITY_H_ -#include "../math/Vector.h" +#include "../graphics/Vector.h" namespace space { diff --git a/src/graphics/Camera.cpp b/src/graphics/Camera.cpp index ea2f97d..74ebd5f 100644 --- a/src/graphics/Camera.cpp +++ b/src/graphics/Camera.cpp @@ -3,9 +3,9 @@ namespace space { -Camera::Camera(int w, int h, const Vector &t) +Camera::Camera(Vector s, const Vector &t) : target(&t) -, size(w, h) +, size(s) , offset(size / 2) , zoom(1) , zoomAcc(0) { @@ -13,8 +13,8 @@ Camera::Camera(int w, int h, const Vector &t) } -void Camera::Resize(int w, int h) { - size = Vector(w, h); +void Camera::Resize(Vector s) { + size = s; offset = size / 2; } diff --git a/src/graphics/Camera.h b/src/graphics/Camera.h index 4d49cb6..815b4b0 100644 --- a/src/graphics/Camera.h +++ b/src/graphics/Camera.h @@ -1,7 +1,7 @@ #ifndef SPACE_CAMERA_H_ #define SPACE_CAMERA_H_ -#include "../math/Vector.h" +#include "Vector.h" namespace space { @@ -9,7 +9,7 @@ namespace space { class Camera { public: - Camera(int w, int h, const Vector &); + Camera(Vector size, const Vector &); public: void SetTarget(const Vector &t) { target = &t; } @@ -18,7 +18,8 @@ public: Vector ScreenSize() const { return size; } float Zoom() const { return zoom; } - void Resize(int w, int h); + void Resize(int w, int h) { Resize(Vector(w, h)); } + void Resize(Vector); void Update(float deltaT); void StartZoom(); diff --git a/src/graphics/Canvas.cpp b/src/graphics/Canvas.cpp new file mode 100644 index 0000000..45997c6 --- /dev/null +++ b/src/graphics/Canvas.cpp @@ -0,0 +1,227 @@ +#include "Canvas.h" + +#include +#include +#include +#include + +using std::runtime_error; + + +namespace space { + +Canvas::Canvas(SDL_Window *win, int index, Uint32 flags) +: canv(SDL_CreateRenderer(win, index, flags)) { + if (!canv) { + throw runtime_error(std::string("create canvas: ") + SDL_GetError()); + } +} + +Canvas::~Canvas() { + if (canv) SDL_DestroyRenderer(canv); +} + +Canvas::Canvas(Canvas &&other) +: canv(other.canv) { + other.canv = nullptr; +} + +Canvas &Canvas::operator =(Canvas &&other) { + std::swap(canv, other.canv); + return *this; +} + + +void Canvas::Present() { + SDL_RenderPresent(canv); +} + + +Vector Canvas::Size() const { + assert(canv); + Vector size; + SDL_GetRendererOutputSize(canv, &size.x, &size.y); + return size; +} + + +void Canvas::SetColor(Color c) { + SDL_SetRenderDrawColor(canv, c.r, c.g, c.b, c.a); +} + + +void Canvas::Fill() { + SDL_RenderClear(canv); +} + +void Canvas::Outline() { + SDL_RenderDrawRect(canv, nullptr); +} + + +void Canvas::Line(Vector from, Vector to) { + SDL_RenderDrawLine(canv, from.x, from.y, to.x, to.y); +} + +void Canvas::FillRect(Vector pos, Vector size) { + SDL_Rect destRect; + destRect.x = pos.x; + destRect.y = pos.y; + destRect.w = size.x; + destRect.h = size.y; + SDL_RenderFillRect(canv, &destRect); +} + +void Canvas::OutlineRect(Vector pos, Vector size) { + SDL_Rect destRect; + destRect.x = pos.x; + destRect.y = pos.y; + destRect.w = size.x; + destRect.h = size.y; + SDL_RenderDrawRect(canv, &destRect); +} + + +void Canvas::Dot(Vector pos) { + SDL_RenderDrawPoint(canv, pos.x, pos.y); +} + +void Canvas::Cross(Vector pos, int extent) { + Line( + Vector(pos.x - extent, pos.y), + Vector(pos.x + extent, pos.y)); + Line( + Vector(pos.x, pos.y - extent), + Vector(pos.x, pos.y + extent)); +} + + +namespace { + +template +void GridImpl( + Canvas &canv, + Vector pos, + Vector size, + Vector step) { + Vector from(pos); + Vector to(pos + size); + Vector clip(canv.Size()); + + if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) { + return; + } + if (step.x <= 1 || step.y <= 1) { + canv.FillRect(pos, size); + return; + } + + if (from.x < -step.x) { + int skip = from.x / -step.x; + from.x += skip * step.x; + } + if (from.y < -step.y) { + int skip = from.y / -step.y; + from.y += skip * step.y; + } + if (to.x > clip.x + step.x) { + int skip = (to.x - clip.x) / step.x; + to.x -= skip * step.x; + } + if (to.y > clip.y + step.y) { + int skip = (to.y - clip.y) / step.y; + to.y -= skip * step.y; + } + + int width = to.x - from.x; + int height = to.y - from.y; + + for (Vector pos(from); pos.x <= to.x; pos.x += step.x) { + canv.Line(pos, Vector(pos.x, pos.y + height)); + } + for (Vector pos(from); pos.y <= to.y; pos.y += step.y) { + canv.Line(pos, Vector(pos.x + width, pos.y)); + } +} + +template +void Grid2Impl( + Canvas &canv, + Vector pos, + Vector size, + Vector step, + Vector n, + Color c1, + Color c2) { + Vector from(pos); + Vector to(pos + size); + Vector clip(canv.Size()); + + if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) { + return; + } + if (step.x <= 1 || step.y <= 1) { + canv.SetColor(c1); + canv.FillRect(pos, size); + canv.SetColor(c2); + GridImpl(canv, pos, size, step); + return; + } + + Vector i(0, 0); + + if (from.x < -step.x) { + int skip = from.x / -step.x; + from.x += skip * step.x; + i.x += skip; + } + if (from.y < -step.y) { + int skip = from.y / -step.y; + from.y += skip * step.y; + i.y += skip; + } + if (to.x > clip.x + step.x) { + int skip = (to.x - clip.x) / step.x; + to.x -= skip * step.x; + } + if (to.y > clip.y + step.y) { + int skip = (to.y - clip.y) / step.y; + to.y -= skip * step.y; + } + + int width = to.x - from.x; + int height = to.y - from.y; + + for (Vector pos(from); pos.x <= to.x; pos.x += step.x) { + canv.SetColor((i.x++ % n.x) ? c1 : c2); + canv.Line(pos, Vector(pos.x, pos.y + height)); + } + for (Vector pos(from); pos.y <= to.y; pos.y += step.y) { + canv.SetColor((i.y++ % n.y) ? c1 : c2); + canv.Line(pos, Vector(pos.x + width, pos.y)); + } +} + +} + +void Canvas::Grid(Vector pos, Vector size, Vector step) { + GridImpl(*this, pos, size, step); +} + +void Canvas::Grid(Vector pos, Vector size, Vector step) { + GridImpl(*this, pos, size, step); +} + +void Canvas::Grid2( + Vector pos, Vector size, Vector step, + Vector n, Color c1, Color c2) { + Grid2Impl(*this, pos, size, step, n, c1, c2); +} + +void Canvas::Grid2( + Vector pos, Vector size, Vector step, + Vector n, Color c1, Color c2) { + Grid2Impl(*this, pos, size, step, n, c1, c2); +} + +} diff --git a/src/graphics/Canvas.h b/src/graphics/Canvas.h new file mode 100644 index 0000000..032c7db --- /dev/null +++ b/src/graphics/Canvas.h @@ -0,0 +1,57 @@ +#ifndef SPACE_CANVAS_H_ +#define SPACE_CANVAS_H_ + +#include "Color.h" +#include "Vector.h" + +#include + + +namespace space { + +class Canvas { + +public: + Canvas() : canv(nullptr) { } + Canvas(SDL_Window *win, int index, Uint32 flags); + ~Canvas(); + + Canvas(Canvas &&); + Canvas &operator =(Canvas &&); + + Canvas(const Canvas &) = delete; + Canvas &operator =(const Canvas &) = delete; + +public: + Vector Size() const; + + void Present(); + + void SetColor(Color); + + void Fill(); + void Outline(); + + void Line(Vector from, Vector to); + void FillRect(Vector pos, Vector size); + void OutlineRect(Vector pos, Vector size); + + void Dot(Vector pos); + void Cross(Vector pos, int extent); + + void Grid(Vector pos, Vector size, Vector step); + void Grid(Vector pos, Vector size, Vector step); + + void Grid2(Vector pos, Vector size, Vector step, + Vector n, Color, Color); + void Grid2(Vector pos, Vector size, Vector step, + Vector n, Color, Color); + +private: + SDL_Renderer *canv; + +}; + +} + +#endif diff --git a/src/graphics/Color.h b/src/graphics/Color.h index d561fc8..9260ad9 100644 --- a/src/graphics/Color.h +++ b/src/graphics/Color.h @@ -1,7 +1,7 @@ #ifndef SPACE_COLOR_H_ #define SPACE_COLOR_H_ -#include +#include namespace space { @@ -13,16 +13,6 @@ struct Color { constexpr Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 0xFF) : r(r), g(g), b(b), a(a) { } - Uint32 MapRGB(SDL_Surface *s) const - { return MapRGB(s->format); } - Uint32 MapRGB(SDL_PixelFormat *f) const - { return SDL_MapRGB(f, r, g, b); } - - Uint32 MapRGBA(SDL_Surface *s) const - { return MapRGBA(s->format); } - Uint32 MapRGBA(SDL_PixelFormat *f) const - { return SDL_MapRGBA(f, r, g, b, a); } - Uint8 r, g, b, a; }; diff --git a/src/graphics/Moveable.h b/src/graphics/Moveable.h index 71f5909..7627283 100644 --- a/src/graphics/Moveable.h +++ b/src/graphics/Moveable.h @@ -1,7 +1,7 @@ #ifndef SPACE_MOVEABLE_H_ #define SPACE_MOVEABLE_H_ -#include "../math/Vector.h" +#include "Vector.h" namespace space { diff --git a/src/math/Vector.h b/src/graphics/Vector.h similarity index 100% rename from src/math/Vector.h rename to src/graphics/Vector.h diff --git a/src/graphics/Window.cpp b/src/graphics/Window.cpp new file mode 100644 index 0000000..7cf1b3a --- /dev/null +++ b/src/graphics/Window.cpp @@ -0,0 +1,58 @@ +#include "Window.h" + +#include +#include +#include +#include + +using std::runtime_error; + + +namespace space { + +const Vector Window::POS_CENTER( + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); +const Vector Window::POS_UNDEF( + SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED); + + +Window::Window( + const char *title, + Vector pos, + Vector size, + Uint32 flags) +: win(SDL_CreateWindow(title, pos.x, pos.y, size.x, size.y, flags)) { + if (!win) { + throw runtime_error(std::string("create window ") + title + + ": " + SDL_GetError()); + } +} + +Window::~Window() { + if (win) SDL_DestroyWindow(win); +} + +Window::Window(Window &&other) +: win(other.win) { + other.win = nullptr; +} + +Window &Window::operator =(Window &&other) { + std::swap(win, other.win); + return *this; +} + + +Vector Window::Size() const { + assert(win); + Vector size; + SDL_GetWindowSize(win, &size.x, &size.y); + return size; +} + + +Canvas Window::CreateCanvas(Uint32 flags) { + return Canvas(win, -1, flags); +} + +} diff --git a/src/graphics/Window.h b/src/graphics/Window.h new file mode 100644 index 0000000..00418af --- /dev/null +++ b/src/graphics/Window.h @@ -0,0 +1,41 @@ +#ifndef SPACE_WINDOW_H_ +#define SPACE_WINDOW_H + +#include "Canvas.h" +#include "Vector.h" + +#include + + +namespace space { + +class Window { + +public: + static const Vector POS_CENTER; + static const Vector POS_UNDEF; + +public: + Window() : win(nullptr) { } + Window(const char *title, Vector pos, Vector size, Uint32 flags); + ~Window(); + + Window(Window &&); + Window &operator =(Window &&); + + Window(const Window &) = delete; + Window &operator =(const Window &) = delete; + +public: + Vector Size() const; + + Canvas CreateCanvas(Uint32 flags); + +private: + SDL_Window *win; + +}; + +} + +#endif diff --git a/src/graphics/primitive.cpp b/src/graphics/primitive.cpp deleted file mode 100644 index f8b5863..0000000 --- a/src/graphics/primitive.cpp +++ /dev/null @@ -1,249 +0,0 @@ -#include "primitive.h" - -#include - -using namespace std; - - -namespace space { - -void Fill(SDL_Surface *dst, Color c) { - Fill(dst, c.MapRGBA(dst)); -} - -void Fill(SDL_Surface *dst, Uint32 color) { - SDL_FillRect(dst, nullptr, color); -} - - -void HLine(SDL_Surface *dst, Vector pos, int len, Color c) { - HLine(dst, pos, len, c.MapRGBA(dst)); -} - -void HLine(SDL_Surface *dst, Vector pos, int len, Uint32 c) { - FillRect(dst, pos, Vector(len, 1), c); -} - -void VLine(SDL_Surface *dst, Vector pos, int len, Color c) { - VLine(dst, pos, len, c.MapRGBA(dst)); -} - -void VLine(SDL_Surface *dst, Vector pos, int len, Uint32 c) { - FillRect(dst, pos, Vector(1, len), c); -} - - -void FillRect(SDL_Surface *dst, Vector pos, Vector size, Color c) { - FillRect(dst, pos, size, c.MapRGBA(dst)); -} - -void FillRect(SDL_Surface *dst, Vector pos, Vector size, Uint32 c) { - SDL_Rect destRect; - destRect.x = pos.x; - destRect.y = pos.y; - destRect.w = size.x; - destRect.h = size.y; - SDL_FillRect(dst, &destRect, c); -} - -void OutlineRect(SDL_Surface *dst, Vector pos, Vector size, Color c) { - OutlineRect(dst, pos, size, c.MapRGBA(dst)); -} - -void OutlineRect(SDL_Surface *dst, Vector pos, Vector size, Uint32 c) { - SDL_Rect destRect; - - destRect.x = pos.x; - destRect.y = pos.y; - destRect.w = size.x; - destRect.h = 1; - SDL_FillRect(dst, &destRect, c); - - destRect.x = pos.x; - destRect.y = pos.y + size.y - 1; - destRect.w = size.x; - destRect.h = 1; - SDL_FillRect(dst, &destRect, c); - - destRect.x = pos.x; - destRect.y = pos.y; - destRect.w = 1; - destRect.h = size.y; - SDL_FillRect(dst, &destRect, c); - - destRect.x = pos.x + size.x - 1; - destRect.y = pos.y; - destRect.w = 1; - destRect.h = size.y; - SDL_FillRect(dst, &destRect, c); -} - - -void Cross( - SDL_Surface *dst, - Vector pos, - int extent, - Color color) { - Uint32 c = color.MapRGBA(dst); - int len = 2 * extent + 1; - - HLine(dst, Vector(pos.x - extent, pos.y), len, c); - VLine(dst, Vector(pos.x, pos.y - extent), len, c); -} - - -void Grid( - SDL_Surface *dst, - Vector first, - Vector second, - Vector size, - Color color) { - Uint32 c = color.MapRGBA(dst); - - Vector from = min(first, second); - Vector to = max(first, second); - - if (size.x <= 1 || size.y <= 1) { - FillRect(dst, from, to - from, c); - return; - } - - if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) { - return; - } - - while (from.x < -size.x) from.x += size.x; - while (from.y < -size.y) from.y += size.y; - while (to.x > dst->w + size.x) to.x -= size.x; - while (to.y > dst->h + size.y) to.y -= size.y; - - int width = to.x - from.x; - int height = to.y - from.y; - - for (Vector pos(from); pos.x <= to.x; pos.x += size.x) { - VLine(dst, pos, height, c); - } - for (Vector pos(from); pos.y <= to.y; pos.y += size.y) { - HLine(dst, pos, width, c); - } -} - -void Grid( - SDL_Surface *dst, - Vector first, - Vector second, - Vector size, - Color color) { - Uint32 c = color.MapRGBA(dst); - - Vector from = min(first, second); - Vector to = max(first, second); - - if (size.x <= 1 || size.y <= 1) { - FillRect(dst, from, to - from, c); - return; - } - - if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) { - return; - } - - while (from.x < -size.x) from.x += size.x; - while (from.y < -size.y) from.y += size.y; - while (to.x > dst->w + size.x) to.x -= size.x; - while (to.y > dst->h + size.y) to.y -= size.y; - - float width = to.x - from.x; - float height = to.y - from.y; - - for (Vector pos(from); pos.x <= to.x; pos.x += size.x) { - VLine(dst, pos, height, c); - } - for (Vector pos(from); pos.y <= to.y; pos.y += size.y) { - HLine(dst, pos, width, c); - } -} - -void Grid2( - SDL_Surface *dst, - Vector first, - Vector second, - Vector size, - Vector n, - Color color1, - Color color2) { - Uint32 c1 = color1.MapRGBA(dst); - Uint32 c2 = color2.MapRGBA(dst); - - Vector from = min(first, second); - Vector to = max(first, second); - - if (size.x <= 1 || size.y <= 1) { - FillRect(dst, from, to - from, c1); - Grid(dst, from, to, size * n, color2); - return; - } - - if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) { - return; - } - - Vector i(0, 0); - while (from.x < -size.x) { from.x += size.x; ++i.x; } - while (from.y < -size.y) { from.y += size.y; ++i.y; } - while (to.x > dst->w + size.x) to.x -= size.x; - while (to.y > dst->h + size.y) to.y -= size.y; - - int width = to.x - from.x; - int height = to.y - from.y; - - for (Vector pos(from); pos.x <= to.x; pos.x += size.x) { - VLine(dst, pos, height, (i.x++ % n.x) ? c1 : c2); - } - for (Vector pos(from); pos.y <= to.y; pos.y += size.y) { - HLine(dst, pos, width, (i.y++ % n.y) ? c1 : c2); - } -} - -void Grid2( - SDL_Surface *dst, - Vector first, - Vector second, - Vector size, - Vector n, - Color color1, - Color color2) { - Uint32 c1 = color1.MapRGBA(dst); - Uint32 c2 = color2.MapRGBA(dst); - - Vector from = min(first, second); - Vector to = max(first, second); - - if (size.x <= 1 || size.y <= 1) { - FillRect(dst, from, to - from, c1); - Grid(dst, from, to, size * Vector(n), color2); - return; - } - - if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) { - return; - } - - Vector i(0, 0); - while (from.x < -size.x) { from.x += size.x; ++i.x; } - while (from.y < -size.y) { from.y += size.y; ++i.y; } - while (to.x > dst->w + size.x) to.x -= size.x; - while (to.y > dst->h + size.y) to.y -= size.y; - - float width = to.x - from.x; - float height = to.y - from.y; - - for (Vector pos(from); pos.x <= to.x; pos.x += size.x) { - VLine(dst, pos, height, (i.x++ % n.x) ? c1 : c2); - } - for (Vector pos(from); pos.y <= to.y; pos.y += size.y) { - HLine(dst, pos, width, (i.y++ % n.y) ? c1 : c2); - } -} - -} diff --git a/src/graphics/primitive.h b/src/graphics/primitive.h deleted file mode 100644 index 6dcc6fe..0000000 --- a/src/graphics/primitive.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef SPACE_PRIMITIVE_H_ -#define SPACE_PRIMITIVE_H_ - -#include "Color.h" -#include "../math/Vector.h" - -#include - - -namespace space { - -void Fill(SDL_Surface *, Color); -void Fill(SDL_Surface *, Uint32 color); - -void HLine(SDL_Surface *, Vector pos, int len, Color); -void HLine(SDL_Surface *, Vector pos, int len, Uint32 color); -void VLine(SDL_Surface *, Vector pos, int len, Color); -void VLine(SDL_Surface *, Vector pos, int len, Uint32 color); - -void FillRect(SDL_Surface *, Vector pos, Vector size, Color); -void FillRect(SDL_Surface *, Vector pos, Vector size, Uint32 color); -void OutlineRect(SDL_Surface *, Vector pos, Vector size, Color); -void OutlineRect(SDL_Surface *, Vector pos, Vector size, Uint32 color); - -void Cross( - SDL_Surface *, - Vector pos, - int extent, - Color); - -void Grid( - SDL_Surface *, - Vector from, - Vector to, - Vector size, - Color); -void Grid( - SDL_Surface *, - Vector from, - Vector to, - Vector size, - Color); - -void Grid2( - SDL_Surface *, - Vector from, - Vector to, - Vector size, - Vector n, - Color, - Color); -void Grid2( - SDL_Surface *, - Vector from, - Vector to, - Vector size, - Vector n, - Color, - Color); - -} - -#endif diff --git a/src/sdl/InitSDL.cpp b/src/sdl/InitSDL.cpp deleted file mode 100644 index 0676dfb..0000000 --- a/src/sdl/InitSDL.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "InitSDL.h" - -#include - -using std::runtime_error; - - -namespace space { - -InitSDL::InitSDL(Uint32 flags) { - if (SDL_Init(flags) != 0) { - throw runtime_error("failed to initialize SDL"); - } -} - -InitSDL::~InitSDL() { - SDL_Quit(); -} - -} diff --git a/src/sdl/InitSDL.h b/src/sdl/InitSDL.h deleted file mode 100644 index dd3cf72..0000000 --- a/src/sdl/InitSDL.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef SPACE_INITSDL_H_ -#define SPACE_INITSDL_H_ - -#include - - -namespace space { - -class InitSDL { - -public: - explicit InitSDL(Uint32 flags = SDL_INIT_EVERYTHING); - ~InitSDL(); - - InitSDL(const InitSDL &) = delete; - InitSDL &operator =(const InitSDL &) = delete; - -}; - -} - -#endif diff --git a/src/sdl/InitScreen.cpp b/src/sdl/InitScreen.cpp deleted file mode 100644 index 2ed13dd..0000000 --- a/src/sdl/InitScreen.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "InitScreen.h" - -#include - -using std::runtime_error; - - -namespace space { - -InitScreen::InitScreen(int width, int height, int bpp, Sint32 flags) -: screen(SDL_SetVideoMode(width, height, bpp, flags)) -, bpp(bpp) -, flags(flags) { - if (!screen) { - throw runtime_error("failed to open screen"); - } -} - -InitScreen::~InitScreen() { - -} - - -SDL_Surface *InitScreen::Resize(int width, int height) { - SDL_Surface *newScreen(SDL_SetVideoMode(width, height, bpp, flags)); - if (!newScreen) { - throw runtime_error("failed to resize screen"); - } - return screen = newScreen; -} - -void InitScreen::Flip() { - SDL_Flip(screen); - if (!(screen->flags & SDL_HWSURFACE)) { - // probably got no vsync, so suspend execution for a while - SDL_Delay(1); - } -} - -} diff --git a/src/sdl/InitScreen.h b/src/sdl/InitScreen.h deleted file mode 100644 index 38ac9da..0000000 --- a/src/sdl/InitScreen.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef SPACE_INITSCREEN_H_ -#define SPACE_INITSCREEN_H_ - -#include - -namespace space { - -class InitScreen { - -public: - InitScreen(int width, int height, int bpp = 32, Sint32 flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE); - ~InitScreen(); - - InitScreen(const InitScreen &) = delete; - InitScreen &operator =(const InitScreen &) = delete; - -public: - SDL_Surface *Screen() { return screen; }; - const SDL_Surface *Screen() const { return screen; }; - - void Flip(); - SDL_Surface *Resize(int width, int height); - -private: - SDL_Surface *screen; - int bpp; - Sint32 flags; - -}; - -} - -#endif diff --git a/src/space.cpp b/src/space.cpp index 5cd6bd7..c3ffb68 100644 --- a/src/space.cpp +++ b/src/space.cpp @@ -1,15 +1,24 @@ #include "app/Application.h" -#include "sdl/InitSDL.h" -#include "sdl/InitScreen.h" +#include "app/SDL.h" +#include "graphics/Canvas.h" +#include "graphics/Window.h" using namespace space; int main(int argc, const char *argv[]) { - InitSDL sdl; - InitScreen screen(800, 600); + SDL sdl(SDL_INIT_VIDEO); + Window win( + "space", + Window::POS_UNDEF, + Vector(800, 600), + SDL_WINDOW_RESIZABLE + ); + Canvas canv(win.CreateCanvas( + 0 + )); - Application app(screen); + Application app(canv); app.Run(); return 0; diff --git a/src/world/Universe.h b/src/world/Universe.h index 7405926..55b86f9 100644 --- a/src/world/Universe.h +++ b/src/world/Universe.h @@ -2,7 +2,7 @@ #define SPACE_UNIVERSE_H_ #include "../entity/Entity.h" -#include "../math/Vector.h" +#include "../graphics/Vector.h" #include -- 2.39.2