lib: update mnit module to the latest FFI spec
[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
18
19 import mnit # for
20 # import opengles1
21
22 import sdl
23
24 in "C Header" `{
25 #define LOGW(...) ((void)fprintf(stderr, "# warn: %s", __VA_ARGS__))
26 #ifdef DEBUG
27 #define LOGI(...) ((void)fprintf(stderr, "# info: %s", __VA_ARGS__))
28 #else
29 #define LOGI(...) (void)0
30 #endif
31 `}
32
33 in "C" `{
34 NativeWindowType mnit_window;
35 EGLNativeDisplayType mnit_native_display;
36
37 SDL_Surface* mnit_sdl_surface;
38 `}
39
40 redef class Display
41 fun wanted_width: Int do return 800
42 fun wanted_height: Int do return 600
43 end
44
45 redef class Opengles1Display # in "C" `{struct mnit_opengles_Texture *`}
46
47 # display managing the window, events, fonts? and image loading?
48 var sdl_display: SDLDisplay
49
50 redef fun extern_init do
51 sdl_display = new SDLDisplay( wanted_width, wanted_height )
52 init_from_sdl( sdl_display )
53 return super
54 end
55
56 fun init_from_sdl( sdl_display: SDLDisplay ): Bool is extern `{
57 mnit_sdl_surface = sdl_display;
58
59 mnit_window = (NativeWindowType)XOpenDisplay(NULL);
60 mnit_native_display = (EGLNativeDisplayType)mnit_window;
61
62 if (!mnit_window)
63 {
64 fprintf(stderr, "ERROR: unable to get display!n");
65 return 3;
66 }
67
68 SDL_SysWMinfo mnit_sys_info;
69 SDL_VERSION(&mnit_sys_info.version);
70 if(SDL_GetWMInfo(&mnit_sys_info) <= 0)
71 {
72 printf("Unable to get window handle");
73 return 0;
74 }
75
76 mnit_window = (EGLNativeWindowType)mnit_sys_info.info.x11.window;
77
78 return 0;
79 `}
80
81 redef fun close
82 do
83 super
84
85 sdl_display.destroy
86 end
87 end
88
89 redef extern Opengles1Image
90 new from_sdl_image( sdl_image: SDLImage ) is extern `{
91 return mnit_opengles_load_image( sdl_image->pixels, sdl_image->w, sdl_image->h, sdl_image->format->Amask );
92 `}
93
94 # using sdl
95 new from_file( path: String ) is extern import String.to_cstring `{
96 SDL_Surface *sdl_image;
97 struct mnit_opengles_Texture *opengles_image;
98
99 sdl_image = IMG_Load( String_to_cstring( path ) );
100 if ( !sdl_image ) {
101 LOGW( "SDL failed to load image <%s>: %s\n", String_to_cstring( path ), IMG_GetError() );
102 return NULL;
103 } else {
104 opengles_image = mnit_opengles_load_image( sdl_image->pixels, sdl_image->w, sdl_image->h, sdl_image->format->Amask );
105 SDL_FreeSurface(sdl_image);
106 return opengles_image;
107 }
108 `}
109 end