2012-01-24 13:16:20 +00:00
|
|
|
#include "system.h"
|
2012-07-23 16:56:24 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
#include <unistd.h>
|
2012-07-23 16:56:24 +00:00
|
|
|
#include <stdlib.h>
|
2012-12-01 16:39:47 +00:00
|
|
|
#include <sys/stat.h>
|
2012-01-24 13:16:20 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
#include "IL/il.h"
|
|
|
|
#include "IL/ilu.h"
|
|
|
|
|
2012-07-01 13:27:57 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#define DEBUG_ONETHREAD 1
|
|
|
|
#endif
|
2012-06-17 16:27:01 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int _core_count = 1;
|
|
|
|
|
|
|
|
static int _getCoreCount()
|
|
|
|
{
|
|
|
|
int core_count = 1;
|
|
|
|
#ifdef WIN32
|
|
|
|
DWORD processAffinityMask;
|
|
|
|
DWORD systemAffinityMask;
|
|
|
|
|
|
|
|
if (GetProcessAffinityMask( GetCurrentProcess(),
|
|
|
|
&processAffinityMask,
|
|
|
|
&systemAffinityMask)){
|
|
|
|
processAffinityMask = (processAffinityMask & 0x55555555)
|
|
|
|
+ (processAffinityMask >> 1 & 0x55555555);
|
|
|
|
processAffinityMask = (processAffinityMask & 0x33333333)
|
|
|
|
+ (processAffinityMask >> 2 & 0x33333333);
|
|
|
|
processAffinityMask = (processAffinityMask & 0x0f0f0f0f)
|
|
|
|
+ (processAffinityMask >> 4 & 0x0f0f0f0f);
|
|
|
|
processAffinityMask = (processAffinityMask & 0x00ff00ff)
|
|
|
|
+ (processAffinityMask >> 8 & 0x00ff00ff);
|
|
|
|
core_count = (processAffinityMask & 0x0000ffff)
|
|
|
|
+ (processAffinityMask >>16 & 0x0000ffff);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef _SC_NPROCESSORS_ONLN
|
|
|
|
core_count = (int)sysconf(_SC_NPROCESSORS_ONLN);
|
2012-01-31 09:48:38 +00:00
|
|
|
#endif
|
|
|
|
#ifdef DEBUG_ONETHREAD
|
|
|
|
core_count = 1;
|
2012-01-24 13:16:20 +00:00
|
|
|
#endif
|
|
|
|
return core_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void systemInit()
|
|
|
|
{
|
2012-11-10 17:05:01 +00:00
|
|
|
#ifndef GLIB_VERSION_2_32
|
2012-01-24 13:16:20 +00:00
|
|
|
g_thread_init(NULL);
|
2012-11-10 17:05:01 +00:00
|
|
|
#endif
|
2012-01-24 13:16:20 +00:00
|
|
|
_core_count = _getCoreCount();
|
2012-07-19 15:04:27 +00:00
|
|
|
ilInit();
|
|
|
|
iluInit();
|
2012-01-24 13:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int systemGetCoreCount()
|
|
|
|
{
|
|
|
|
return _core_count;
|
|
|
|
}
|
2012-07-19 15:04:27 +00:00
|
|
|
|
|
|
|
int systemSavePictureFile(const char* filepath, PictureCallbackSavePixel callback_pixel, void* data, int width, int height)
|
|
|
|
{
|
|
|
|
ILuint image_id;
|
|
|
|
Color result;
|
|
|
|
ILuint x, y;
|
|
|
|
ILuint rgba;
|
|
|
|
ILuint pixels[width * height];
|
|
|
|
ILenum error;
|
|
|
|
int error_count;
|
|
|
|
|
|
|
|
ilGenImages(1, &image_id);
|
|
|
|
ilBindImage(image_id);
|
|
|
|
|
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
result = callback_pixel(data, x, y);
|
|
|
|
rgba = colorTo32BitRGBA(&result);
|
|
|
|
pixels[y * width + x] = rgba;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ilTexImage((ILuint)width, (ILuint)height, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, pixels);
|
|
|
|
remove(filepath);
|
|
|
|
ilSaveImage(filepath);
|
|
|
|
|
|
|
|
ilDeleteImages(1, &image_id);
|
|
|
|
|
|
|
|
error_count = 0;
|
|
|
|
while ((error=ilGetError()) != IL_NO_ERROR)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "IL ERROR : %s\n", iluErrorString(error));
|
|
|
|
error_count++;
|
|
|
|
}
|
|
|
|
return !error_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int systemLoadPictureFile(const char* filepath, PictureCallbackLoadStarted callback_start, PictureCallbackLoadPixel callback_pixel, void* data)
|
|
|
|
{
|
|
|
|
ILuint image_id;
|
|
|
|
ILenum error;
|
|
|
|
int error_count;
|
|
|
|
int width, height;
|
|
|
|
ILuint* pixels;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
ilGenImages(1, &image_id);
|
|
|
|
ilBindImage(image_id);
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
if (ilLoadImage(filepath))
|
|
|
|
{
|
|
|
|
width = ilGetInteger(IL_IMAGE_WIDTH);
|
|
|
|
height = ilGetInteger(IL_IMAGE_HEIGHT);
|
|
|
|
callback_start(data, width, height);
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
pixels = malloc(sizeof(ILuint) * width * height);
|
|
|
|
ilCopyPixels(0, 0, 0, width, height, 1, IL_RGBA, IL_UNSIGNED_BYTE, pixels);
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
callback_pixel(data, x, y, colorFrom32BitRGBA(pixels[y * width + x]));
|
|
|
|
}
|
|
|
|
}
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
free(pixels);
|
|
|
|
}
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
error_count = 0;
|
|
|
|
while ((error=ilGetError()) != IL_NO_ERROR)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "IL ERROR : %s\n", iluErrorString(error));
|
|
|
|
error_count++;
|
|
|
|
}
|
|
|
|
return !error_count;
|
|
|
|
}
|
2012-12-01 16:39:47 +00:00
|
|
|
|
|
|
|
int systemGetFileSize(const char* path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
stat(path, &st);
|
|
|
|
return st.st_size;
|
|
|
|
}
|