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