osx_env.mm 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // osx_env.mm
  3. // gameutils
  4. //
  5. // Created by Sam Jaffe on 5/19/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include "game/util/env.hpp"
  9. #import <Cocoa/Cocoa.h>
  10. #include "vector/vector.hpp"
  11. namespace {
  12. NSUInteger encoding = NSUTF8StringEncoding;
  13. NSString* translate(std::string const & str) {
  14. return [NSString stringWithCString:str.c_str() encoding:encoding];
  15. }
  16. }
  17. namespace env {
  18. std::string resource_file(std::string const& path) {
  19. size_t dir_idx = path.find_last_of("/");
  20. size_t ext_idx = path.find_first_of(".");
  21. std::string base = path.substr(0, dir_idx);
  22. std::string name = path.substr(dir_idx + 1, ext_idx - (dir_idx + 1));
  23. std::string type = path.substr(ext_idx + 1);
  24. NSString* url = [[NSBundle mainBundle] pathForResource:translate(name)
  25. ofType:translate(type)
  26. inDirectory:translate(base)];
  27. char const* abs_path = [url cStringUsingEncoding:encoding];
  28. return abs_path ? std::string(abs_path) : std::string();
  29. }
  30. namespace detail {
  31. math::vec2i init_screen() {
  32. NSRect frame = [[NSView focusView] frame];
  33. return make_vector(static_cast<int>(frame.size.width),
  34. static_cast<int>(frame.size.height));
  35. }
  36. math::vec2i screenSize = init_screen();
  37. void resize_screen(math::vec2i const& size) {
  38. screenSize = size;
  39. }
  40. }
  41. math::vec2i screen_resolution() {
  42. return detail::screenSize * 2; // TODO: aquire this programmatically
  43. }
  44. math::vec2i screen_size() {
  45. return detail::screenSize;
  46. }
  47. void resize_screen(math::vec2i const& size) {
  48. NSSize sz = NSMakeSize(size[0], size[1]);
  49. [[NSView focusView] setFrameSize:sz];
  50. detail::resize_screen(size);
  51. }
  52. }