Closed Mem leak on DisplaFormat functions as they return a copy of the surface.
[sdlgit/SDL_perl.git] / src / Core / objects / Surface.xs
CommitLineData
88a46efc 1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
5#ifndef aTHX_
6#define aTHX_
7#endif
8
9#include <SDL.h>
10
11MODULE = SDL::Surface PACKAGE = SDL::Surface PREFIX = surface_
12
13=for documentation
14
15SDL_Surface -- Graphic surface structure
16
17 typedef struct SDL_Surface {
18 Uint32 flags; /* Read-only */
19 SDL_PixelFormat *format; /* Read-only */
20 int w, h; /* Read-only */
21 Uint16 pitch; /* Read-only */
22 void *pixels; /* Read-write */
23 SDL_Rect clip_rect; /* Read-only */
24 int refcount; /* Read-mostly */
25
26 /* This structure also contains private fields not shown here */
27 } SDL_Surface;
28
29=cut
30
31SDL_Surface *
32surface_new (CLASS, flags, width, height, depth, Rmask, Gmask, Bmask, Amask )
33 char* CLASS
34 Uint32 flags
35 int width
36 int height
37 int depth
38 Uint32 Rmask
39 Uint32 Gmask
40 Uint32 Bmask
41 Uint32 Amask
42 CODE:
43 RETVAL = SDL_CreateRGBSurface ( flags, width, height,
44 depth, Rmask, Gmask, Bmask, Amask );
45 OUTPUT:
46 RETVAL
47
50d9130a 48SDL_PixelFormat *
49surface_format ( surface )
50 SDL_Surface *surface
51 CODE:
52 char* CLASS = "SDL::PixelFormat";
53 RETVAL = surface->format;
54 OUTPUT:
55 RETVAL
56
88a46efc 57void
88a46efc 58surface_update_rects ( surface, ... )
59 SDL_Surface *surface
60 CODE:
61 SDL_Rect *rects, *temp;
62 int num_rects,i;
63 if ( items < 2 ) return;
64 num_rects = items - 1;
65
66 rects = (SDL_Rect *)safemalloc(sizeof(SDL_Rect)*items);
67 for(i=0;i<num_rects;i++) {
68 temp = (SDL_Rect *)SvIV(ST(i+1));
69 rects[i].x = temp->x;
70 rects[i].y = temp->y;
71 rects[i].w = temp->w;
72 rects[i].h = temp->h;
73 }
74 SDL_UpdateRects(surface,num_rects,rects);
75 safefree(rects);
76
77SDL_Surface *
78surface_display ( surface )
79 SDL_Surface *surface
80 CODE:
41a5a9ee 81 char* CLASS = "SDL::Surface";
8fb66c78 82
83 SDL_Surface *new_surface = SDL_DisplayFormat(surface);
84 SDL_FreeSurface(surface);
85 RETVAL = new_surface;
88a46efc 86 OUTPUT:
87 RETVAL
88
41a5a9ee 89SDL_Surface *
90surface_display_alpha ( surface )
91 SDL_Surface *surface
92 CODE:
93 char* CLASS = "SDL::Surface";
8fb66c78 94 SDL_Surface *new_surface = SDL_DisplayFormatAlpha(surface);
95 SDL_FreeSurface(surface);
96 RETVAL = new_surface;
41a5a9ee 97 OUTPUT:
98 RETVAL
99
88a46efc 100Uint16
18ef5a29 101surface_pitch( surface )
102 SDL_Surface *surface
103 CODE:
104 RETVAL = surface->pitch;
105 OUTPUT:
106 RETVAL
107
108Uint16
88a46efc 109surface_w ( surface )
110 SDL_Surface *surface
111 CODE:
112 RETVAL = surface->w;
113 OUTPUT:
114 RETVAL
115
116Uint16
117surface_h ( surface )
118 SDL_Surface *surface
119 CODE:
120 RETVAL = surface->h;
121 OUTPUT:
122 RETVAL
123
18ef5a29 124IV
125surface_get_pixels(surface)
126 SDL_Surface *surface
127 CODE:
b7ed9095 128 if(!surface->pixels) croak("Incomplete surface");
793e8806 129 RETVAL = PTR2IV(surface->pixels);
18ef5a29 130 OUTPUT:
793e8806 131 RETVAL
18ef5a29 132
133void
134surface_set_pixels(surface, pixels)
135 SDL_Surface *surface
136
137 SV *pixels
138
139 PREINIT:
140 STRLEN len;
141 void *p;
142
143 CODE:
144 p = SvPV(pixels, len);
145 if (len > surface->pitch*surface->h)
146 len = surface->pitch*surface->h;
147 memcpy(surface->pixels, p, len);
148
88a46efc 149void
150surface_DESTROY(surface)
151 SDL_Surface *surface
152 CODE:
153 Uint8* pixels = surface->pixels;
154 Uint32 flags = surface->flags;
155 SDL_FreeSurface(surface);
156 if (flags & SDL_PREALLOC)
157 Safefree(pixels);