From b01871799afefe84ec7ef856101334fda0be33a8 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Fri, 23 Oct 2020 12:43:47 +0200 Subject: [PATCH] first commit --- Makefile | 84 +++++++++++++++++++++++++++++++++++++++++++++ SDLImageWrapper.cpp | 23 +++++++++++++ SDLImageWrapper.hpp | 23 +++++++++++++ SDLWrapper.cpp | 18 ++++++++++ SDLWrapper.hpp | 23 +++++++++++++ Window.cpp | 64 ++++++++++++++++++++++++++++++++++ Window.hpp | 34 ++++++++++++++++++ 7 files changed, 269 insertions(+) create mode 100644 Makefile create mode 100644 SDLImageWrapper.cpp create mode 100644 SDLImageWrapper.hpp create mode 100644 SDLWrapper.cpp create mode 100644 SDLWrapper.hpp create mode 100644 Window.cpp create mode 100644 Window.hpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4ae5cfd --- /dev/null +++ b/Makefile @@ -0,0 +1,84 @@ +LIBNAME := $(shell basename $$(pwd)) +MAJOR := 1 +MINOR := 0.0 + +UNAME_S := $(shell uname -s) + +ifeq ($(UNAME_S),Darwin) + LINKERNAME := $(LIBNAME).dylib + SONAME := $(LIBNAME).$(MAJOR).dylib + REALNAME := $(LINKERNAME) +else + LINKERNAME := $(LIBNAME).so + SONAME := $(LINKERNAME).$(MAJOR) + REALNAME := $(SONAME).$(MINOR) +endif + +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin +CONFIGDIR ?= $(PREFIX)/etc +INCLUDEDIR ?= $(PREFIX)/include +LIBDIR ?= $(PREFIX)/lib +DATADIR ?= $(PREFIX)/share +MANDIR ?= $(DATADIR)/man +DOCDIR ?= $(DATADIR)/$(LIBNAME)/doc + +SRCS := $(wildcard *.cpp) +OBJS := $(subst .cpp,.o,$(SRCS)) +DEPS := $(subst .cpp,.d,$(SRCS)) +HDRS := $(filter-out $(LIBNAME).hpp,$(wildcard *.hpp)) + +CXX ?= g++ + +CXXFLAGS := $(CXXFLAGS) -Wshadow -Wall -Wpedantic -Wextra -g -fno-strict-aliasing -std=c++17 -fPIC +ifeq ($(DEBUG),1) + CXXFLAGS += -D DEBUG -O0 +else + CXXFLAGS += -D NDEBUG -O3 +endif + +LDLIBS := -lSDL2 -lSDL2_image + +RM := /bin/rm -f +INSTALL := /usr/bin/install -c + +.PHONY: all clean install + +all: $(REALNAME) + +$(REALNAME): $(OBJS) $(DEPS) +ifeq ($(UNAME_S),Darwin) + $(CXX) -dynamiclib -o $(REALNAME) -current_version $(MAJOR) -compatibility_version $(MINOR) $(LDFLAGS) $(LDLIBS) $(OBJS) +else + $(CXX) -g -shared -Wl,-soname,$(SONAME) -o $(REALNAME) $(LDFLAGS) $(LDLIBS) $(OBJS) +endif + +%.o: %.cpp %.d Makefile + $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< + +-include *.d + +%.d: ; + +$(LIBNAME).hpp: $(HDRS) + @echo updating $(LIBNAME).hpp + @cp /dev/null $(LIBNAME).hpp + @for h in $(HDRS); \ + do \ + cat $$h >> $(LIBNAME).hpp; \ + done + +clean: + $(RM) $(OBJS) $(DEPS) $(REALNAME) $(LIBNAME).hpp + +install: $(REALNAME) $(LIBNAME).hpp + $(INSTALL) -d $(LIBDIR) + $(INSTALL) -m 644 $(REALNAME) $(LIBDIR) + $(INSTALL) -d $(INCLUDEDIR) + $(INSTALL) -m 644 $(LIBNAME).hpp $(INCLUDEDIR) +ifeq ($(UNAME_S),Darwin) + cd $(LIBDIR) && ln -sf $(REALNAME) $(SONAME) +else + ldconfig + cd $(LIBDIR) && ln -sf $(SONAME) $(LINKERNAME) +endif diff --git a/SDLImageWrapper.cpp b/SDLImageWrapper.cpp new file mode 100644 index 0000000..413c09d --- /dev/null +++ b/SDLImageWrapper.cpp @@ -0,0 +1,23 @@ +// +// SDLImageWrapper.cpp +// gui +// +// Created by Bob Polis at 2020-10-14 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#include +#include +#include "SDLImageWrapper.hpp" + +SDLImageWrapper::SDLImageWrapper() { + int flags = IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF; + int result = IMG_Init(flags); + if ((result & flags) != flags) { + throw std::runtime_error(IMG_GetError()); + } +} + +SDLImageWrapper::~SDLImageWrapper() { + IMG_Quit(); +} diff --git a/SDLImageWrapper.hpp b/SDLImageWrapper.hpp new file mode 100644 index 0000000..5c1924a --- /dev/null +++ b/SDLImageWrapper.hpp @@ -0,0 +1,23 @@ +// +// SDLImageWrapper.hpp +// gui +// +// Created by Bob Polis at 2020-10-14 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#ifndef __SDLIMAGEWRAPPER_H_ +#define __SDLIMAGEWRAPPER_H_ + +struct SDLImageWrapper { + SDLImageWrapper(); + ~SDLImageWrapper(); + + // prohibit copy and move + SDLImageWrapper(const SDLImageWrapper&) = delete; + SDLImageWrapper& operator=(const SDLImageWrapper&) = delete; + SDLImageWrapper(SDLImageWrapper&&) = delete; + SDLImageWrapper& operator=(SDLImageWrapper&&) = delete; +}; + +#endif // __SDLIMAGEWRAPPER_H_ diff --git a/SDLWrapper.cpp b/SDLWrapper.cpp new file mode 100644 index 0000000..4e696b2 --- /dev/null +++ b/SDLWrapper.cpp @@ -0,0 +1,18 @@ +// +// SDLWrapper.cpp +// gui +// +// Created by Bob Polis at 2020-10-14 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#include +#include "SDLWrapper.hpp" + +SDLWrapper::SDLWrapper() { + SDL_Init(SDL_INIT_VIDEO); +} + +SDLWrapper::~SDLWrapper() { + SDL_Quit(); +} diff --git a/SDLWrapper.hpp b/SDLWrapper.hpp new file mode 100644 index 0000000..a9433e1 --- /dev/null +++ b/SDLWrapper.hpp @@ -0,0 +1,23 @@ +// +// SDLWrapper.hpp +// gui +// +// Created by Bob Polis at 2020-10-14 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#ifndef __SDLWRAPPER_H_ +#define __SDLWRAPPER_H_ + +struct SDLWrapper { + SDLWrapper(); + ~SDLWrapper(); + + // prohibit copy and move + SDLWrapper(const SDLWrapper&) = delete; + SDLWrapper& operator=(const SDLWrapper&) = delete; + SDLWrapper(SDLWrapper&&) = delete; + SDLWrapper& operator=(SDLWrapper&&) = delete; +}; + +#endif // __SDLWRAPPER_H_ diff --git a/Window.cpp b/Window.cpp new file mode 100644 index 0000000..8264632 --- /dev/null +++ b/Window.cpp @@ -0,0 +1,64 @@ +// +// Window.cpp +// gui +// +// Created by Bob Polis at 2020-10-14 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#include +#include +#include "Window.hpp" + +Window::Window(const char* title) : _path {title} { + SDL_Window* win = SDL_CreateWindow(title, + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + 900, 540, + SDL_WINDOW_RESIZABLE); + _w.reset(win); + _r.reset(SDL_CreateRenderer(win, -1, 0)); +} + +void Window::set_size(int w, int h) { + SDL_SetWindowSize(_w.get(), w, h); +} + +void Window::update() const { + SDL_RenderClear(_r.get()); + SDL_RenderCopy(_r.get(), _t.get(), nullptr, nullptr); + SDL_RenderPresent(_r.get()); +} + +void Window::load_image() { + // load image from file into surface + SDL_Surface* loaded = IMG_Load(_path.c_str()); + if (!loaded) throw std::runtime_error(IMG_GetError()); + std::unique_ptr loaded_ {loaded, SDL_FreeSurface}; + + // create texture from surface + _t.reset(SDL_CreateTextureFromSurface(_r.get(), loaded)); + + // query texture for properties, like image size + Uint32 format; + int access; + int w, h; + int result = SDL_QueryTexture(_t.get(), &format, &access, &w, &h); + if (result != 0) throw std::runtime_error(SDL_GetError()); + + // get screen size, to scale down if image is too big + SDL_DisplayMode dm; + SDL_GetCurrentDisplayMode(0, &dm); + if (dm.w < w || dm.h < h) { + double screen_ratio {static_cast(dm.w) / dm.h}; + double image_ratio {static_cast(w) / h}; + const int safety {100}; // room for window and desktop adornments + if (screen_ratio > image_ratio) { // screen relatively less high than image + h = dm.h - safety; + w = static_cast(round(image_ratio * h)); + } else { // screen relatively less wide than image + w = dm.w - safety; + h = static_cast(round(w / image_ratio)); + } + } + set_size(w, h); +} diff --git a/Window.hpp b/Window.hpp new file mode 100644 index 0000000..79b53bf --- /dev/null +++ b/Window.hpp @@ -0,0 +1,34 @@ +// +// Window.hpp +// gui +// +// Created by Bob Polis at 2020-10-14 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#ifndef __WINDOW_H_ +#define __WINDOW_H_ + +#include +#include +#include +#include + +class Window { + public: + Window(const char* title); + + SDL_Window* get_window() const { return _w.get(); } + void set_size(int w, int h); + + void update() const; + void load_image(); + + private: + std::string _path; + std::unique_ptr _w {nullptr, SDL_DestroyWindow}; + std::unique_ptr _r {nullptr, SDL_DestroyRenderer}; + std::unique_ptr _t {nullptr, SDL_DestroyTexture}; +}; + +#endif // __WINDOW_H_