| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- //
- // osx_env.mm
- // gameutils
- //
- // Created by Sam Jaffe on 5/19/19.
- // Copyright © 2019 Sam Jaffe. All rights reserved.
- //
- #include "game/util/env.hpp"
- #import <Cocoa/Cocoa.h>
- #include "vector/vector.hpp"
- namespace {
- NSUInteger encoding = NSUTF8StringEncoding;
- NSString* translate(std::string const & str) {
- return [NSString stringWithCString:str.c_str() encoding:encoding];
- }
- }
- namespace env {
- std::string resource_file(std::string const& path) {
- size_t dir_idx = path.find_last_of("/");
- size_t ext_idx = path.find_first_of(".");
- std::string base = path.substr(0, dir_idx);
- std::string name = path.substr(dir_idx + 1, ext_idx - (dir_idx + 1));
- std::string type = path.substr(ext_idx + 1);
-
- NSString* url = [[NSBundle mainBundle] pathForResource:translate(name)
- ofType:translate(type)
- inDirectory:translate(base)];
-
- char const* abs_path = [url cStringUsingEncoding:encoding];
- return abs_path ? std::string(abs_path) : std::string();
- }
-
- namespace detail {
- math::vec2i init_screen() {
- NSRect frame = [[NSView focusView] frame];
- return make_vector(static_cast<int>(frame.size.width),
- static_cast<int>(frame.size.height));
- }
-
- math::vec2i screenSize = init_screen();
-
- void resize_screen(math::vec2i const& size) {
- screenSize = size;
- }
- }
-
- math::vec2i screen_resolution() {
- return detail::screenSize * 2; // TODO: aquire this programmatically
- }
-
- math::vec2i screen_size() {
- return detail::screenSize;
- }
-
- void resize_screen(math::vec2i const& size) {
- NSSize sz = NSMakeSize(size[0], size[1]);
- [[NSView focusView] setFrameSize:sz];
- detail::resize_screen(size);
- }
- }
|