Add proper initializers to common classes

master
Piotr Dziwinski 2015-08-06 14:26:30 +02:00
parent 2906451d06
commit 654ef7ba59
3 changed files with 52 additions and 64 deletions

View File

@ -551,14 +551,14 @@ enum EventType
struct KeyEventData struct KeyEventData
{ {
//! If true, the key is a virtual code generated by certain key modifiers or joystick buttons //! If true, the key is a virtual code generated by certain key modifiers or joystick buttons
bool virt; bool virt = false;
//! Key symbol: KEY(...) macro value or virtual key VIRTUAL_... (from common/key.h) //! Key symbol: KEY(...) macro value or virtual key VIRTUAL_... (from common/key.h)
unsigned int key; unsigned int key = 0;
//! Unicode character //! Unicode character
//! NOTE: applicable only to EVENT_KEY_DOWN events! //! NOTE: applicable only to EVENT_KEY_DOWN events!
unsigned int unicode; unsigned int unicode = 0;
//! Input binding slot for this key //! Input binding slot for this key
InputSlot slot; InputSlot slot = {};
}; };
/** /**
@ -583,7 +583,7 @@ enum MouseButton
struct MouseButtonEventData struct MouseButtonEventData
{ {
//! The mouse button //! The mouse button
MouseButton button; MouseButton button = {};
}; };
/** /**
@ -603,7 +603,7 @@ enum WheelDirection
struct MouseWheelEventData struct MouseWheelEventData
{ {
//! Wheel direction //! Wheel direction
WheelDirection dir; WheelDirection dir = {};
}; };
/** /**
@ -613,9 +613,9 @@ struct MouseWheelEventData
struct JoyAxisEventData struct JoyAxisEventData
{ {
//! The joystick axis index //! The joystick axis index
unsigned char axis; unsigned char axis = 0;
//! The axis value (range: -32768 to 32767) //! The axis value (range: -32768 to 32767)
int value; int value = 0;
}; };
/** /**
@ -625,7 +625,7 @@ struct JoyAxisEventData
struct JoyButtonEventData struct JoyButtonEventData
{ {
//! The joystick button index //! The joystick button index
unsigned char button; unsigned char button = 0;
}; };
/** /**
@ -650,9 +650,9 @@ enum ActiveEventFlags
struct ActiveEventData struct ActiveEventData
{ {
//! Flags (bitmask of enum values ActiveEventFlags) //! Flags (bitmask of enum values ActiveEventFlags)
unsigned char flags; unsigned char flags = 0;
//! True if the focus was gained; false otherwise //! True if the focus was gained; false otherwise
bool gain; bool gain = false;
}; };

View File

@ -20,14 +20,16 @@
#include "common/image.h" #include "common/image.h"
#include "common/make_unique.h"
#include "common/resources/resourcemanager.h" #include "common/resources/resourcemanager.h"
#include "math/func.h" #include "math/func.h"
#include <stdlib.h> #include <cstdlib>
#include <stdio.h> #include <cstdio>
#include <string.h> #include <cstring>
#include <assert.h> #include <cassert>
#include <SDL.h> #include <SDL.h>
#include <SDL_image.h> #include <SDL_image.h>
@ -65,7 +67,10 @@
you can find other examples on http://marsnomercy.org you can find other examples on http://marsnomercy.org
*/ */
namespace
{
std::string PNG_ERROR = ""; std::string PNG_ERROR = "";
}
void PNGUserError(png_structp ctx, png_const_charp str) void PNGUserError(png_structp ctx, png_const_charp str)
{ {
@ -86,16 +91,10 @@ int PNGColortypeFromSurface(SDL_Surface *surface)
bool PNGSaveSurface(const char *filename, SDL_Surface *surf) bool PNGSaveSurface(const char *filename, SDL_Surface *surf)
{ {
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
int i, colortype;
png_bytep *row_pointers;
PNG_ERROR = ""; PNG_ERROR = "";
/* Opening output file */ /* Opening output file */
fp = fopen(filename, "wb"); FILE *fp = fopen(filename, "wb");
if (fp == nullptr) if (fp == nullptr)
{ {
PNG_ERROR = std::string("Could not open file '") + std::string(filename) + std::string("' for saving"); PNG_ERROR = std::string("Could not open file '") + std::string(filename) + std::string("' for saving");
@ -103,48 +102,46 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf)
} }
/* Initializing png structures and callbacks */ /* Initializing png structures and callbacks */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, PNGUserError, nullptr); png_structp pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, PNGUserError, nullptr);
if (png_ptr == nullptr) if (pngPtr == nullptr)
{ {
fclose(fp); fclose(fp);
return false; return false;
} }
info_ptr = png_create_info_struct(png_ptr); png_infop infoPtr = png_create_info_struct(pngPtr);
if (info_ptr == nullptr) if (infoPtr == nullptr)
{ {
png_destroy_write_struct(&png_ptr, static_cast<png_infopp>(nullptr)); png_destroy_write_struct(&pngPtr, static_cast<png_infopp>(nullptr));
PNG_ERROR = "png_create_info_struct() error!"; PNG_ERROR = "png_create_info_struct() error!";
fclose(fp); fclose(fp);
return false; return false;
} }
if (setjmp(png_jmpbuf(png_ptr))) if (setjmp(png_jmpbuf(pngPtr)))
{ {
png_destroy_write_struct(&png_ptr, &info_ptr); png_destroy_write_struct(&pngPtr, &infoPtr);
fclose(fp); fclose(fp);
return false; return false;
} }
png_init_io(png_ptr, fp); png_init_io(pngPtr, fp);
colortype = PNGColortypeFromSurface(surf); int colortype = PNGColortypeFromSurface(surf);
png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h, 8, colortype, PNG_INTERLACE_NONE, png_set_IHDR(pngPtr, infoPtr, surf->w, surf->h, 8, colortype, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
/* Writing the image */ /* Writing the image */
png_write_info(png_ptr, info_ptr); png_write_info(pngPtr, infoPtr);
png_set_packing(png_ptr); png_set_packing(pngPtr);
row_pointers = new png_bytep[surf->h]; auto rowPointers = MakeUniqueArray<png_bytep>(surf->h);
for (i = 0; i < surf->h; i++) for (int i = 0; i < surf->h; i++)
row_pointers[i] = static_cast<png_bytep>( static_cast<Uint8 *>(surf->pixels) ) + i*surf->pitch; rowPointers[i] = static_cast<png_bytep>( static_cast<Uint8 *>(surf->pixels) ) + i*surf->pitch;
png_write_image(png_ptr, row_pointers); png_write_image(pngPtr, rowPointers.get());
png_write_end(png_ptr, info_ptr); png_write_end(pngPtr, infoPtr);
/* Cleaning out... */ png_destroy_write_struct(&pngPtr, &infoPtr);
delete[] row_pointers;
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp); fclose(fp);
return true; return true;
@ -160,7 +157,7 @@ CImage::CImage()
CImage::CImage(Math::IntPoint size) CImage::CImage(Math::IntPoint size)
{ {
m_data = new ImageData(); m_data = MakeUnique<ImageData>();
m_data->surface = SDL_CreateRGBSurface(0, size.x, size.y, 32, m_data->surface = SDL_CreateRGBSurface(0, size.x, size.y, 32,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
} }
@ -184,14 +181,13 @@ void CImage::Free()
SDL_FreeSurface(m_data->surface); SDL_FreeSurface(m_data->surface);
m_data->surface = nullptr; m_data->surface = nullptr;
} }
delete m_data; m_data.reset();
m_data = nullptr;
} }
} }
ImageData* CImage::GetData() ImageData* CImage::GetData()
{ {
return m_data; return m_data.get();
} }
Math::IntPoint CImage::GetSize() const Math::IntPoint CImage::GetSize() const
@ -331,7 +327,7 @@ void CImage::SetPixelInt(Math::IntPoint pixel, Gfx::IntColor color)
Uint32 u = SDL_MapRGBA(m_data->surface->format, color.r, color.g, color.b, color.a); Uint32 u = SDL_MapRGBA(m_data->surface->format, color.r, color.g, color.b, color.a);
switch(bpp) switch (bpp)
{ {
case 1: case 1:
*p = u; *p = u;
@ -386,15 +382,14 @@ bool CImage::Load(const std::string& fileName)
if (! IsEmpty() ) if (! IsEmpty() )
Free(); Free();
m_data = new ImageData(); m_data = MakeUnique<ImageData>();
m_error = ""; m_error = "";
auto file = CResourceManager::GetSDLFileHandler(fileName.c_str()); auto file = CResourceManager::GetSDLFileHandler(fileName.c_str());
if (!file->IsOpen()) if (!file->IsOpen())
{ {
delete m_data; m_data.reset();
m_data = nullptr;
m_error = "Unable to open file"; m_error = "Unable to open file";
return false; return false;
@ -402,8 +397,7 @@ bool CImage::Load(const std::string& fileName)
m_data->surface = IMG_Load_RW(file->GetHandler(), 1); m_data->surface = IMG_Load_RW(file->GetHandler(), 1);
if (m_data->surface == nullptr) if (m_data->surface == nullptr)
{ {
delete m_data; m_data.reset();
m_data = nullptr;
m_error = std::string(IMG_GetError()); m_error = std::string(IMG_GetError());
return false; return false;

View File

@ -24,12 +24,11 @@
#pragma once #pragma once
#include "graphics/core/color.h" #include "graphics/core/color.h"
#include "math/intpoint.h" #include "math/intpoint.h"
#include <stddef.h> #include <memory>
#include <string> #include <string>
@ -42,9 +41,7 @@ struct SDL_Surface;
struct ImageData struct ImageData
{ {
//! SDL surface with image data //! SDL surface with image data
SDL_Surface* surface; SDL_Surface* surface = nullptr;
ImageData() { surface = NULL; }
}; };
/** /**
@ -56,12 +53,6 @@ struct ImageData
*/ */
class CImage class CImage
{ {
private:
//! Blocked!
CImage(const CImage &other) {}
//! Blocked!
void operator=(const CImage &other) {}
public: public:
//! Constructs empty image (with NULL data) //! Constructs empty image (with NULL data)
CImage(); CImage();
@ -70,6 +61,9 @@ public:
//! Destroys image, calling Free() //! Destroys image, calling Free()
virtual ~CImage(); virtual ~CImage();
CImage(const CImage &other) = delete;
void operator=(const CImage &other) = delete;
//! Frees the allocated image data //! Frees the allocated image data
void Free(); void Free();
@ -125,6 +119,6 @@ private:
//! Last encountered error //! Last encountered error
std::string m_error; std::string m_error;
//! Image data //! Image data
ImageData* m_data; std::unique_ptr<ImageData> m_data;
}; };