Compiling the game and completing resource loading

The first thing I worked on this week was finishing up the code that does resource loading which has now been moved to the LowLevelResources class inside of the resources folder. After doing that I worked on bringing in the game code. This was easier than expected with only a few problems with conversions from strings of wchar_t, used mainly for file code, to char strings. I then did some testing and got to load the game.cfg file, which is part of the game’s assets. After that the code to instantiate the various systems gets executed and the program crashed by trying to dereference a null pointer.

Next, I worked on replacing image loading. This was done by the LowLevelResourcesSDL class through the SDL image library. The data was then stored in the SDLBitmap2D class in the form of SDL_surface. Everything has now been moved to the Bitmap2D class in the graphics folder which also handles image loading through the Image::*Decoder family of classes. One problem I encountered was that after loading an image the relative Graphics::Surface cannot be modified, which is required by the fillRect and drawToBitmap methods. This led to the implementation of a copy-on-write mechanism.

This week the codebase was also modified to reduce the number of warnings produced by gcc. After bringing in the engine code the total lines of warnings were about 24000. A lot of these where caused by missing virtual destructors in classes that had virtual methods. After adding these, the line count was reduced to 3000. With more changes, it’s now at less than 2000, which is a huge improvement. The credit for this work goes to sev with me doing only very minor changes.

The last thing I worked on this week was font loading. The game uses a format split into a .fnt xml file and some tga image files which contain a table of characters. This concludes resource loading for now, there’s still the save system but this has been pushed to a later date since it’s not necessary to get the game working.

Bonus problems

  • After including pixelformat.h in some files, the compiler started generating some weird errors which I couldn’t figure out. Turn out the problem was that hpl1/engine/graphics/PixelFormat.h and graphics/pixelformat.h were considered the same file. I don’t know what happened with the directories but for the files, this was caused by the windows file system being case insensitive.
  • In the bitmap class I used Common::ScopedPtr for storing the image decoder. Trying to use the move assignment operator though generated compilation errors. The operation is implemented as:
    template<class T2>
    ScopedPtr &operator=(ScopedPtr<T2> &&other) {
    	PointerType oldPointer = _pointer;
    	_pointer = other._pointer;
    	other._pointer = nullptr;
    	DL()(oldPointer);
    	return *this;
    }
    

    The problem turned out to be that different istances of the same template class cannot access each other’s private members. The fix was a friend declaration in Common::ScopedPtr to all the other instances of itself:

    template<class T2, class DL2>
    friend ScopedPtr; 
    

Next week

Next week I’ll start working the graphics system.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *