lib/mnit: expand images to the nearest power of 2 for OpenGL ES 1.0
[nit.git] / lib / mnit_linux / linux_opengles1.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 module linux_opengles1 is pkgconfig("x11")
18
19 import mnit::opengles1
20
21 import sdl
22
23 in "C" `{
24 NativeWindowType mnit_window;
25 EGLNativeDisplayType mnit_native_display;
26
27 SDL_Surface* mnit_sdl_surface;
28 `}
29
30 redef class Display
31 fun wanted_width: Int do return 800
32 fun wanted_height: Int do return 600
33 end
34
35 redef class Opengles1Display
36
37 # display managing the window, events, fonts? and image loading?
38 var sdl_display: SDLDisplay
39
40 redef fun extern_init do
41 sdl_display = new SDLDisplay( wanted_width, wanted_height )
42 init_from_sdl( sdl_display )
43 return super
44 end
45
46 fun init_from_sdl( sdl_display: SDLDisplay ): Bool is extern `{
47 mnit_sdl_surface = sdl_display;
48
49 mnit_window = (NativeWindowType)XOpenDisplay(NULL);
50 mnit_native_display = (EGLNativeDisplayType)mnit_window;
51
52 if (!mnit_window)
53 {
54 fprintf(stderr, "ERROR: unable to get display!n");
55 return 3;
56 }
57
58 SDL_SysWMinfo mnit_sys_info;
59 SDL_VERSION(&mnit_sys_info.version);
60 if(SDL_GetWMInfo(&mnit_sys_info) <= 0)
61 {
62 printf("Unable to get window handle");
63 return 0;
64 }
65
66 mnit_window = (EGLNativeWindowType)mnit_sys_info.info.x11.window;
67
68 return 0;
69 `}
70
71 redef fun close
72 do
73 super
74
75 sdl_display.destroy
76 end
77 end
78
79 redef extern class Opengles1Image
80 new from_sdl_image( sdl_image: SDLImage ) is extern `{
81 return mnit_opengles_load_image( sdl_image->pixels,
82 sdl_image->w, sdl_image->h,
83 sdl_image->w, sdl_image->h, sdl_image->format->Amask );
84 `}
85
86 # using sdl
87 new from_file( path: String ) is extern import String.to_cstring `{
88 SDL_Surface *sdl_image;
89 struct mnit_opengles_Texture *opengles_image;
90
91 sdl_image = IMG_Load( String_to_cstring( path ) );
92 if ( !sdl_image ) {
93 fprintf(stderr, "SDL failed to load image <%s>: %s\n", String_to_cstring(path), IMG_GetError());
94 return NULL;
95 } else {
96 opengles_image = mnit_opengles_load_image( sdl_image->pixels,
97 sdl_image->w, sdl_image->h,
98 sdl_image->w, sdl_image->h, sdl_image->format->Amask );
99 SDL_FreeSurface(sdl_image);
100 return opengles_image;
101 }
102 `}
103 end