first commit
This commit is contained in:
commit
b01871799a
84
Makefile
Normal file
84
Makefile
Normal file
@ -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
|
23
SDLImageWrapper.cpp
Normal file
23
SDLImageWrapper.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// SDLImageWrapper.cpp
|
||||
// gui
|
||||
//
|
||||
// Created by Bob Polis at 2020-10-14
|
||||
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
||||
//
|
||||
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <stdexcept>
|
||||
#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();
|
||||
}
|
23
SDLImageWrapper.hpp
Normal file
23
SDLImageWrapper.hpp
Normal file
@ -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_
|
18
SDLWrapper.cpp
Normal file
18
SDLWrapper.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// SDLWrapper.cpp
|
||||
// gui
|
||||
//
|
||||
// Created by Bob Polis at 2020-10-14
|
||||
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
||||
//
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "SDLWrapper.hpp"
|
||||
|
||||
SDLWrapper::SDLWrapper() {
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
}
|
||||
|
||||
SDLWrapper::~SDLWrapper() {
|
||||
SDL_Quit();
|
||||
}
|
23
SDLWrapper.hpp
Normal file
23
SDLWrapper.hpp
Normal file
@ -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_
|
64
Window.cpp
Normal file
64
Window.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
//
|
||||
// Window.cpp
|
||||
// gui
|
||||
//
|
||||
// Created by Bob Polis at 2020-10-14
|
||||
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
||||
//
|
||||
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <cmath>
|
||||
#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<SDL_Surface, void(*)(SDL_Surface*)> 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<double>(dm.w) / dm.h};
|
||||
double image_ratio {static_cast<double>(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<int>(round(image_ratio * h));
|
||||
} else { // screen relatively less wide than image
|
||||
w = dm.w - safety;
|
||||
h = static_cast<int>(round(w / image_ratio));
|
||||
}
|
||||
}
|
||||
set_size(w, h);
|
||||
}
|
34
Window.hpp
Normal file
34
Window.hpp
Normal file
@ -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 <SDL2/SDL.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
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<SDL_Window, void(*)(SDL_Window*)> _w {nullptr, SDL_DestroyWindow};
|
||||
std::unique_ptr<SDL_Renderer, void(*)(SDL_Renderer*)> _r {nullptr, SDL_DestroyRenderer};
|
||||
std::unique_ptr<SDL_Texture, void(*)(SDL_Texture*)> _t {nullptr, SDL_DestroyTexture};
|
||||
};
|
||||
|
||||
#endif // __WINDOW_H_
|
Loading…
x
Reference in New Issue
Block a user