File &File::write(const void *ptr, Size size, Size count) { if (handle == nullptr) { D6_THROW(IoException, "Writing to a closed stream"); } if (fwrite(ptr, size, count, handle) != count) { D6_THROW(IoException, "Not all data was correctly written"); } return *this; }
File &File::read(void *ptr, Size size, Size count) { if (handle == nullptr) { D6_THROW(IoException, "Reading from a closed stream"); } if (fread(ptr, size, count, handle) != count) { D6_THROW(IoException, "Insufficient data in input stream"); } return *this; }
void Application::setup(Int32 argc, char** argv) { srand((unsigned)time(nullptr)); if (SDL_Init(SDL_INIT_VIDEO) != 0) { D6_THROW(VideoException, Format("Unable to set graphics mode: {0}") << SDL_GetError()); } if (TTF_Init() != 0) { D6_THROW(FontException, Format("Unable to initialize font subsystem: {0}") << TTF_GetError()); } // Print application info SDL_version sdlVersion; console.printLine("\n===Application information==="); console.printLine(Format("{0} version: {1}") << APP_NAME << APP_VERSION); SDL_GetVersion(&sdlVersion); console.printLine(Format("SDL version: {0}.{1}.{2}") << sdlVersion.major << sdlVersion.minor << sdlVersion.patch); const SDL_version* mixVersion = Mix_Linked_Version(); console.printLine(Format("SDL_mixer version: {0}.{1}.{2}") << mixVersion->major << mixVersion->minor << mixVersion->patch); const SDL_version* ttfVersion = TTF_Linked_Version(); console.printLine(Format("SDL_ttf version: {0}.{1}.{2}") << ttfVersion->major << ttfVersion->minor << ttfVersion->patch); Console::registerBasicCommands(console); ConsoleCommands::registerCommands(console, service, menu, gameSettings); Math::initialize(); console.printLine("\n===Font initialization==="); font.load(D6_FILE_TTF_FONT, console); video.initialize(APP_NAME, APP_FILE_ICON, console); menu.initialize(); gameResources = std::make_unique<GameResources>(service); game = std::make_unique<Game>(service, *gameResources, gameSettings); menu.setGameReference(game.get()); for (Weapon weapon : Weapon::values()) { gameSettings.enableWeapon(weapon, true); } // Execute config script and command line arguments console.printLine("\n===Config==="); console.exec(std::string("exec ") + D6_FILE_CONFIG); for (int i = 1; i < argc; i++) { console.exec(argv[i]); } }
void Console::verifyRegistration(const std::string &proc, const std::string &name, bool isNull) { if (name.empty()) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : cannot register empty name")) << proc); } if (findCommand(name) != nullptr || findVar(name) != nullptr || findAlias(name) != nullptr) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : name \"{1}\" is already registered")) << proc << name); } if (isNull) { D6_THROW(ConsoleException, CON_Format(CON_Lang("{0} : attempt to register \"{1}\" with null pointer")) << proc << name); } }
bool File::isEof() const { if (handle == nullptr) { D6_THROW(IoException, "Querying closed stream for end of file"); } return (feof(handle) != 0); }
Size File::countFiles(const std::string& path, const std::string& extension) { DIR* handle = opendir(path.c_str()); if (handle == nullptr) { D6_THROW(IoException, "Invalid directory specified: " + path); } Size count = 0; struct dirent* ff = readdir(handle); while (ff != nullptr) { bool pseudoDir = (!strcmp(ff->d_name, ".") || !strcmp(ff->d_name, "..")); if (!pseudoDir && nameEndsWith(ff->d_name, extension)) { ++count; } ff = readdir(handle); } closedir(handle); return count; }
void File::listDirectory(const std::string& path, const std::string& extension, std::vector<std::string>& fileNames) { fileNames.clear(); DIR* handle = opendir(path.c_str()); if (handle == nullptr) { D6_THROW(IoException, "Invalid directory specified: " + path); } struct dirent* ff = readdir(handle); while (ff != nullptr) { bool pseudoDir = (!strcmp(ff->d_name, ".") || !strcmp(ff->d_name, "..")); if (!pseudoDir && nameEndsWith(ff->d_name, extension)) { fileNames.push_back(ff->d_name); } ff = readdir(handle); } closedir(handle); }
std::string saveScreenTga(const Video& video) { Int32 num = 0; std::string name; // Vyhledani cisla pod ktere ukladat while (num < 1000) { num++; name = Format("screenshot_{0}.tga") << num; if (!File::exists(name)) { break; } } // Maximalne 1000 screenshotu if (num >= 1000) { D6_THROW(IoException, "Maximum number of 1000 screenshots reached"); } Image image(video.getScreen().getClientWidth(), video.getScreen().getClientHeight()); glReadPixels(0, 0, video.getScreen().getClientWidth(), video.getScreen().getClientHeight(), GL_RGBA, GL_UNSIGNED_BYTE, &image.at(0)); saveTarga(name, image); return name; }
void Video::initialize(const std::string& name, const std::string& icon, Console& console) { console.printLine("\n===Video initialization==="); // Get current video mode SDL_DisplayMode currentVideoMode; if (SDL_GetCurrentDisplayMode(0, ¤tVideoMode)) { D6_THROW(VideoException, std::string("Unable to determine current video mode: ") + SDL_GetError()); } // Set graphics mode view = ViewParameters(0.1f, 100.0f, 45.0f); #ifdef D6_DEBUG // Running fullscren makes switching to debugger problematic with SDL (focus is captured) screen = ScreenParameters(1280, 900, 32, 0, false); #else screen = ScreenParameters(currentVideoMode.w, currentVideoMode.h, 32, 0, true); #endif window = createWindow(name, icon, screen, console); glContext = createContext(screen, console); SDL_ShowCursor(SDL_DISABLE); setMode(Mode::Orthogonal); }
bool Format::getPlaceholder(Size index, Placeholder& placeholder) { std::string prefix = '{' + std::to_string(index); size_t start = format.find(prefix); if (start == std::string::npos) { return false; } size_t end = format.find('}', start + prefix.length()); if (end == std::string::npos) { D6_THROW(FormatException, "Unclosed parameter placeholder in: " + format); } placeholder.offset = start; placeholder.length = end - start + 1; if (end == start + prefix.length()) { placeholder.hasWidth = false; return true; } else if (format[start + prefix.length()] == ',') { size_t widthStart = start + prefix.length() + 1; placeholder.hasWidth = true; std::string widthSpec = format.substr(widthStart, end - widthStart); size_t padDelimitPos = widthSpec.find('|'); if (padDelimitPos != std::string::npos && padDelimitPos + 2 == widthSpec.length()) { placeholder.paddingCharacter = format[end - 1]; widthSpec = widthSpec.substr(0, padDelimitPos); } else { placeholder.paddingCharacter = ' '; } placeholder.width = std::stoi(widthSpec); return true; } D6_THROW(FormatException, "Wrong format of parameter placeholder in: " + format); return true; }
const std::shared_ptr<ValueImpl>& get(const std::string& propertyName) const override { auto val = properties.find(propertyName); if (val == properties.end()) { D6_THROW(JsonException, std::string("Property ") + propertyName + " not found"); } return val->second; }
File &File::open(const std::string &path, Mode mode, Access access) { close(); std::string fileMode = Format("{0}{1}") << (access == Access::Read ? 'r' : 'w') << (mode == Mode::Text ? 't' : 'b'); handle = fopen(path.c_str(), fileMode.c_str()); if (handle == nullptr) { D6_THROW(IoException, "Unable to open file: " + path); } return *this; }
File& File::seek(long offset, Seek seek) { if (handle == nullptr) { D6_THROW(IoException, "Trying to seek in a closed stream"); } int origin; switch (seek) { case Seek::Set: origin = SEEK_SET; break; case Seek::End: origin = SEEK_END; break; default: origin = SEEK_CUR; break; } if (fseek(handle, offset, origin)) { D6_THROW(IoException, "Seek operation failed"); } return *this; }
Size File::getSize(const std::string &path) { FILE *f = fopen(path.c_str(), "rb"); if (f == nullptr) { return 0; } if (fseek(f, 0, SEEK_END)) { D6_THROW(IoException, "Unable to rewind to the end of a stream"); } Size length = (Size) ftell(f); fclose(f); return length; }
SDL_GLContext Video::createContext(const ScreenParameters& params, Console& console) { SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); SDL_GL_SetAttribute (SDL_GL_BUFFER_SIZE, params.getBitsPerPixel()); SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); if (params.getAntiAlias() > 0) { console.printLine(Format("...Anti-aliasing: {0}x") << params.getAntiAlias()); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, params.getAntiAlias()); } console.printLine("...Creating OpenGL context"); console.printLine(Format("...Bits per-pixel: {0}") << params.getBitsPerPixel()); SDL_GLContext glc = SDL_GL_CreateContext(window); if (glc == nullptr) { D6_THROW(VideoException, std::string("Unable to OpenGL context: ") + SDL_GetError()); } // Retrieve final video parameters int val[7]; SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &val[0]); SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &val[1]); SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &val[2]); SDL_GL_GetAttribute(SDL_GL_BUFFER_SIZE, &val[3]); SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &val[4]); SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &val[5]); SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &val[6]); console.printLine(Format("...RGB ({0}, {1}, {2})") << val[0] << val[1] << val[2]); console.printLine(Format("...Color ({0}), Z-buffer ({1}), Alpha channel ({2}), Stencil ({3})") << val[3] << val[4] << val[5] << val[6]); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glCullFace(GL_FRONT); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); return glc; }
SDL_Window* Video::createWindow(const std::string& name, const std::string& icon, const ScreenParameters& params, Console& console) { Uint32 flags = 0; flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_INPUT_GRABBED; if (params.isFullScreen()) { flags |= SDL_WINDOW_FULLSCREEN; } console.printLine(Format("...Creating SDL window: {0}x{1}") << params.getClientWidth() << params.getClientHeight()); SDL_Window* sdlWin = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, params.getClientWidth(), params.getClientHeight(), flags); if (sdlWin == nullptr) { D6_THROW(VideoException, std::string("Unable to create application window: ") + SDL_GetError()); } SDL_SetWindowTitle(sdlWin, name.c_str()); SDL_SetWindowIcon(sdlWin, SDL_LoadBMP(icon.c_str())); return sdlWin; }
void set(const Size index, std::shared_ptr<ValueImpl>& value) override { D6_THROW(JsonException, std::string("Invalid JSON value type - expected: Array, got: ") + getTypeName()); }
std::vector<std::string> getPropertyNames() const override { D6_THROW(JsonException, std::string("Invalid JSON value type - expected: Object, got: ") + getTypeName()); }
void set(const std::string& propertyName, std::shared_ptr<ValueImpl>& value) override { D6_THROW(JsonException, std::string("Invalid JSON value type - expected: Object, got: ") + getTypeName()); }
virtual bool asBoolean() const override { D6_THROW(JsonException, std::string("Invalid JSON value type - expected: Boolean, got: ") + getTypeName()); }
virtual Float64 asDouble() const override { D6_THROW(JsonException, std::string("Invalid JSON value type - expected: Number, got: ") + getTypeName()); }
virtual std::string asString() const override { D6_THROW(JsonException, std::string("Invalid JSON value type - expected: String, got: ") + getTypeName()); }
virtual Size getLength() const override { D6_THROW(JsonException, std::string("Invalid JSON value type - expected: Array, got: ") + getTypeName()); }