-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.e
136 lines (118 loc) · 3.59 KB
/
application.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
note
description: "[
Eiffel SDL application that shows event handling system.
For original C version, please see:
http://lazyfoo.net/tutorials/SDL/03_event_driven_programming/index.php
]"
date: "$Date$"
revision: "$Revision$"
EIS: "name=Event Driven", "src=http://lazyfoo.net/tutorials/SDL/03_event_driven_programming/index.php", "protocol=uri"
class
APPLICATION
inherit
ARGUMENTS_32
create
make
feature {NONE} -- Initialization
make
-- Run application.
local
res: INTEGER
quit: BOOLEAN
event: SDL_EVENT_UNION_API
-- event handler.
do
init
load_media
if attached x_out as l_x_out and then
attached screen_surface as l_screen_surface and then
attached window as l_window
then
from
-- While the application is running.
until
quit
loop
from
-- Handle events on the queue.
create event.make
until
{SDL_EVENTS}.sdl_poll_event (event) = 0
loop
-- User requests Quit.
if event.type = {SDL_CONSTANT_API}.SDL_QUIT then
quit := True
end
end
-- Apply the image
res := {SDL_SURFACE}.sdl_upper_blit (l_x_out, Void, l_screen_surface, Void)
res := {SDL_VIDEO_FUNCTIONS_API}.sdl_update_window_surface (l_window)
end
end
close
end
init
-- Starts up SDL and creates window
do
-- Initialize SDL
if {SDL_FUNCTIONS_API}.sdl_init ({SDL_CONSTANT_API}.SDL_INIT_VIDEO) < 0 then
print ("SDL could not initialize! SDL_Error: " + {SDL_ERROR}.sdl_get_error);
{EXCEPTIONS}.die (1)
else
window := {SDL_VIDEO}.sdl_create_window ("SDL Tutorial", {SDL_CONSTANT_API}.SDL_WINDOWPOS_UNDEFINED, {SDL_CONSTANT_API}.SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, {SDL_WINDOW_FLAGS_ENUM_API}.SDL_WINDOW_SHOWN)
if attached window as l_window then
screen_surface := {SDL_VIDEO}.sdl_get_window_surface (l_window)
-- Get window surface
if screen_surface = Void then
print ("Surface could not be getted! SDL_Error: " + {SDL_ERROR}.sdl_get_error)
{EXCEPTIONS}.die (1)
end
else
print ("Window could not be created! SDL_Error: " + {SDL_ERROR}.sdl_get_error)
{EXCEPTIONS}.die (1)
end
end
end
load_media
-- loads media.
local
l_ptr: POINTER
rwops: SDL_RWOPS_STRUCT_API
do
l_ptr := {SDL_RWOPS_FUNCTIONS_API}.sdl_rwfrom_file ((create {C_STRING}.make ("x.bmp")).item, (create {C_STRING}.make ("rb")).item)
if l_ptr /= default_pointer then
create rwops.make_by_pointer (l_ptr)
x_out := {SDL_SURFACE_FUNCTIONS_API}.sdl_load_bmp_rw (rwops, 1)
if x_out = Void then
print ("Unable to load image 02_getting_an_image_on_the_screen/hello_world.bmp " + {SDL_ERROR}.sdl_get_error + "%N")
{EXCEPTIONS}.die (1)
end
else
print ("Unable to load image 02_getting_an_image_on_the_screen/hello_world.bmp " + {SDL_ERROR}.sdl_get_error + "%N")
{EXCEPTIONS}.die (1)
end
end
close
-- Clean up the code
do
if attached x_out as l_hello_world then
{SDL_SURFACE_FUNCTIONS_API}.sdl_free_surface (l_hello_world)
end
if attached window as l_window then
{SDL_VIDEO_FUNCTIONS_API}.sdl_destroy_window (l_window)
end
{SDL_FUNCTIONS_API}.sdl_quit
end
feature -- Access
window: detachable SDL_WINDOW_STRUCT_API
-- The window we'll be rendering to.
screen_surface: detachable SDL_SURFACE_STRUCT_API
-- The surface contained by the window.
x_out: detachable SDL_SURFACE_STRUCT_API
-- The image we will load and show on the screen.
feature {NONE} -- Implementation
SCREEN_WIDTH: INTEGER = 640
SCREEN_HEIGHT: INTEGER = 480
-- Screen dimension constants.
end