diff --git a/gb.sdl2/AUTHORS b/gb.sdl2/AUTHORS index 27dce17c9..e69de29bb 100644 --- a/gb.sdl2/AUTHORS +++ b/gb.sdl2/AUTHORS @@ -1,6 +0,0 @@ -Codes/ideas/contributions (in alphabetic order): - -Fabien Bodard -Daniel Campos FernànDez -Laurent Carlier -Benoît Minisini diff --git a/gb.sdl2/gb.geom.h b/gb.sdl2/gb.geom.h new file mode 120000 index 000000000..abc5659ee --- /dev/null +++ b/gb.sdl2/gb.geom.h @@ -0,0 +1 @@ +../main/lib/geom/gb.geom.h \ No newline at end of file diff --git a/gb.sdl2/src/c_image.c b/gb.sdl2/src/c_image.c new file mode 100644 index 000000000..77b5a1d14 --- /dev/null +++ b/gb.sdl2/src/c_image.c @@ -0,0 +1,197 @@ +/*************************************************************************** + + c_image.c + + (c) 2014 Benoît Minisini + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. + +***************************************************************************/ + +#define __C_IMAGE_C + +#include "c_image.h" + +#define THIS ((CIMAGE *)_object) +#define THIS_IMAGE (&THIS->img) + +static SDL_Image *SDL_CreateImage(SDL_Surface *surface) +{ + SDL_Image *image; + + GB.Alloc(POINTER(&image), sizeof(SDL_Image)); + + image->texture = NULL; + image->window_id = 0; + image->surface = surface; + + return image; +} + +static void SDL_FreeImage(SDL_Image *image) +{ + if (image->texture) + SDL_DestroyTexture(image->texture); + if (image->surface) + SDL_FreeSurface(image->surface); + GB.Free(POINTER(&image)); +} + +static void free_image(GB_IMG *img, void *image) +{ + SDL_FreeImage((SDL_Image *)image); +} + +static void *temp_image(GB_IMG *img) +{ + SDL_Surface *surface = NULL; + + if (img && img->data) + surface = SDL_CreateRGBSurfaceFrom(img->data, img->width, img->height, 32, img->width * sizeof(int), RMASK, GMASK, BMASK, AMASK); + + return SDL_CreateImage(surface); +} + +/*static void sync_image(GB_IMG *image) +{ + // Synchronize l'image texture->image + + // Puis mets le flag de synchro à false + image->sync = false; +}*/ + +static GB_IMG_OWNER _image_owner = { + "gb.sdl2", + DEFAULT_IMAGE_FORMAT, + free_image, + free_image, + temp_image, + NULL, //sync_image, + }; + +SDL_Image *IMAGE_get(CIMAGE *_object) +{ + return (SDL_Image *)IMAGE.Check(THIS_IMAGE, &_image_owner); +} + +#define check_image IMAGE_get + +static void take_image(CIMAGE *_object, SDL_Image *image) +{ + if (image && image->surface) + IMAGE.Take(THIS_IMAGE, &_image_owner, image, image->surface->w, image->surface->h, image->surface->pixels); + else + IMAGE.Take(THIS_IMAGE, &_image_owner, image, 0, 0, NULL); +} + +CIMAGE *IMAGE_create(SDL_Image *image) +{ + CIMAGE *img; + + img = (CIMAGE *)GB.New(CLASS_Image, NULL, NULL); + take_image(img, image); + return img; +} + +SDL_Texture *IMAGE_get_texture(CIMAGE *_object, CWINDOW *window) +{ + SDL_Image *image = IMAGE_get(THIS); + + if (image->texture && image->window_id != window->id) + { + SDL_DestroyTexture(image->texture); + image->texture = NULL; + } + + if (!image->texture) + { + image->texture = SDL_CreateTextureFromSurface(window->renderer, image->surface); + SDL_SetTextureBlendMode(image->texture, SDL_BLENDMODE_BLEND); + image->window_id = window->id; + } + + return image->texture; +} + +/*CIMAGE *CIMAGE_create_from_window(SDLwindow *window, int x, int y, int w, int h) +{ + GB_IMG *img; + uchar *line, *top, *bottom; + uint size; + + if (w < 0) + w = window->GetWidth(); + + if (h < 0) + h = window->GetHeight(); + + if (w <= 0 || h <= 0) + return NULL; + + img = IMAGE.Create(w, h, GB_IMAGE_RGBA, NULL); + glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, img->data); + + size = img->width * sizeof(uint); + GB.Alloc(POINTER(&line), size); + top = img->data; + bottom = img->data + img->height * size; + + for (y = 0; y < img->height / 2; y++) + { + bottom -= size; + memcpy(line, top, size); + memcpy(top, bottom, size); + memcpy(bottom, line, size); + top += size; + } + + GB.Free(POINTER(&line)); + + return (CIMAGE *)img; +}*/ + +//------------------------------------------------------------------------- + +BEGIN_METHOD(Image_Load, GB_STRING path) + + char *addr; + int len; + SDL_Surface *surface; + + if (GB.LoadFile(STRING(path), LENGTH(path), &addr, &len)) + return; + + surface = IMG_Load_RW(SDL_RWFromConstMem(addr, len), TRUE); + if (!surface) + { + GB.Error("Unable to load image: &1", IMG_GetError()); + return; + } + + GB.ReturnObject(IMAGE_create(SDL_CreateImage(surface))); + +END_METHOD + +//------------------------------------------------------------------------- + +GB_DESC ImageDesc[] = +{ + GB_DECLARE("Image", sizeof(CIMAGE)), + + GB_STATIC_METHOD("Load", "Image", Image_Load, "(Path)s"), + + GB_END_DECLARE +}; diff --git a/gb.sdl2/src/c_image.h b/gb.sdl2/src/c_image.h new file mode 100644 index 000000000..c91d42efd --- /dev/null +++ b/gb.sdl2/src/c_image.h @@ -0,0 +1,69 @@ +/*************************************************************************** + + c_image.h + + (c) 2014 Benoît Minisini + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. + +***************************************************************************/ + +#ifndef __C_IMAGE_H +#define __C_IMAGE_H + +#include "main.h" +#include "c_window.h" + +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + +#define DEFAULT_IMAGE_FORMAT GB_IMAGE_ARGB +#define RMASK 0xFF000000 +#define GMASK 0x00FF0000 +#define BMASK 0x0000FF00 +#define AMASK 0x000000FF + +#else + +#define DEFAULT_IMAGE_FORMAT GB_IMAGE_BGRA +#define RMASK 0x000000FF +#define GMASK 0x0000FF00 +#define BMASK 0x00FF0000 +#define AMASK 0xFF000000 + +#endif + +typedef + struct { + SDL_Surface *surface; + SDL_Texture *texture; + int window_id; + } + SDL_Image; + +typedef + struct { + GB_IMG img; + } + CIMAGE; + +#ifndef __C_IMAGE_C +extern GB_DESC ImageDesc[]; +#endif + +SDL_Texture *IMAGE_get_texture(CIMAGE *_object, CWINDOW *window); + +#endif /* __C_IMAGE_H */ +