Merge lp:~der-treba/stratagus/stratagus into lp:stratagus

Proposed by treba
Status: Needs review
Proposed branch: lp:~der-treba/stratagus/stratagus
Merge into: lp:stratagus
Diff against target: 360 lines (+188/-85)
5 files modified
src/game/game.cpp (+1/-0)
src/include/video.h (+2/-0)
src/stratagus/stratagus.cpp (+3/-0)
src/video/sdl.cpp (+108/-85)
src/video/video.cpp (+74/-0)
To merge this branch: bzr merge lp:~der-treba/stratagus/stratagus
Reviewer Review Type Date Requested Status
Pali Needs Fixing
treba Pending
Review via email: mp+159972@code.launchpad.net

Description of the change

Hello,

i made some small changes to the engine that allow us to get the native resolution of the screen within lua and let's us determine all available resolutions.

examples:

local defaultPreferences = {
    ...
    VideoWidth = GetCurrentResolutionsWidth(),
    VideoHeight = GetCurrentResolutionsHeight(),
    ...
}

for i=0,GetVideoResolutionsNr()-1 do
 print(GetResolutionsWidth(i), GetResolutionsHeight(i))
end

i hope the code quality is ok. i only tested it on linux, but to me, it seems to be quite solid.

To post a comment you must log in.
lp:~der-treba/stratagus/stratagus updated
8698. By treba

- removed declarations of lua bindings from video.h
- replaced resolution calls with the following two:
 GetCurrentResolution() -- array with width and height of current resolution. if called at startup, the optimal resolution is given
 GetAvailableResolutions() -- two dimensional array with available resolutions. in window mode gives nil

Revision history for this message
treba (der-treba) wrote :

ok, i removed the stuff from the header and replaced the calls with two simple ones:

GetCurrentResolution() -- array with width and height of current resolution. if called at startup, the optimal resolution is given

GetAvailableResolutions() -- two dimensional array with available resolutions. in window mode gives nil

i guess it's ready now

Revision history for this message
Travis (dinky-dye-aussie) wrote :

So where do I put this code to make it work in the game? I would like to see what this code does so how can we get it working?

Revision history for this message
Joris Dauphin (joris-dauphin) wrote :

Look better.
Maybe GetCurrentResolution may return two int instead of a table to allow:

videoWidth, videoHeight = GetCurrentResolution()

It seems that atexit(SDL_Quit) may be called twice.

Revision history for this message
treba (der-treba) wrote :

> So where do I put this code to make it work in the game? I would like to see
> what this code does so how can we get it working?

i'm not sure if i'm getting your question, but if it is about how you can use it inside the game:

well, you can use it for example in the stratagus.lua:
...
local defaultPreferences = {
    ...
    VideoWidth = GetCurrentResolution()[1],
    VideoHeight = GetCurrentResolution()[2],
    ...
}
...

that will give you your native resolution as default. so if the game supports it and you don't specify a particular resolution, you will always have the native one, regardles what monitor you are using.

GetAvailableResolutions allows to build a dynamic list of resolutions in the options menu.
this will print them to stdout:

if GetAvailableResolutions() then
 for _,v in ipairs(GetAvailableResolutions()) do
  print(v[1]..'x'..v[2])
 end
end

Revision history for this message
treba (der-treba) wrote :

> Look better.
> Maybe GetCurrentResolution may return two int instead of a table to allow:
>
> videoWidth, videoHeight = GetCurrentResolution()
>
>
>
> It seems that atexit(SDL_Quit) may be called twice.

for the first part: good idea.

the second one: i didn't really get what atexit(SDL_Quit) actually does (and when). can you help me with that one?

Revision history for this message
Travis (dinky-dye-aussie) wrote :

Yeah this things isn't working. I got an stderr output for you. I placed the lines you detailed treba in the stratagus.lua file in the preferences sectoion, as follows.

local defaultPreferences = {
 CampaignHuman = 1,
 CampaignHumanX = 1,
 CampaignOrc = 1,
 CampaignOrcX = 1,
 DoubleClickDelayInMs = 300,
 EffectsEnabled = true,
 EffectsVolume = 128,
 EnableKeyboardScrolling = true,
 EnableMouseScrolling = true,
 FogOfWar = true,
 FogOfWarOpacity = 128,
 GameSpeed = 30,
 GameTranslation = "",
 GrabMouse = false,
 GroupKeys = "0123456789`",
 HoldClickDelayInMs = 1000,
 LeaveStopScrolling = true,
 MaxOpenGLTexture = 0,
 MinimapWithTerrain = true,
 MouseScrollSpeed = 1,
 MouseScrollSpeedControl = 15,
 MouseScrollSpeedDefault = 4,
 MusicEnabled = true,
 MusicVolume = 128,
 PlayerName = "Player",
 ServerIP = "localhost",
 ShowCommandKey = true,
 ShowTips = true,
 StratagusTranslation = "",
 TipNumber = 0,
 UseFancyBuildings = true,
 UseOpenGL = false,
 VideoFullScreen = true,
 VideoWidth = GetCurrentResolution(),
        VideoHeight = GetCurrentResolution(),
}

See, right at the bottom ^^^^ I tried running wargus. didn't even start. Here's the stderr readout.

[string "C:/Games/Wargus/scripts/stratagus.lua"]:312: attempt to call global 'GetCurrentResolution' (a nil value)
stack traceback:
 [string "C:/Games/Wargus/scripts/stratagus.lua"]:312: in main chunk

I'll try the "GetAvailableResolutions()" parameter see if that works...

...Nope, no luck with that either.

Revision history for this message
treba (der-treba) wrote :

> Yeah this things isn't working. I got an stderr output for you. I placed the
> lines you detailed treba in the stratagus.lua file in the preferences
> sectoion, as follows.
>

ahm yeah, that's the point of the merge. i implemented this calls in the c code i want to merge. you have to compile my branch to test it. if it was already in the engine, i wouldn't have to merge new code for it^^

Revision history for this message
Kyran Jackson (erstmap) wrote :

That's our Travis!

Revision history for this message
Travis (dinky-dye-aussie) wrote :

I downloaded your trunk code and the code isn't in the files you list it being in...sooooo I dunno where it is...

Kyran man...You burned me good mate :)

Revision history for this message
Joris Dauphin (joris-dauphin) wrote :

@treba:
atexit is the global c Raii.
atexit(SDL_Quit) mean that SDL_Quit will be called when program exits.
Call it twice means that SQL_Quit will be invoked twice when program exits.
So, I think you may remove the other atexit(SDL_Quit).

Revision history for this message
treba (der-treba) wrote :

> @treba:
> atexit is the global c Raii.
> atexit(SDL_Quit) mean that SDL_Quit will be called when program exits.
> Call it twice means that SQL_Quit will be invoked twice when program exits.
> So, I think you may remove the other atexit(SDL_Quit).

no, the second call is done in the if (SDL_WasInit(SDL_INIT_VIDEO) == 0) block. so it's only called once (just checked it again)

> I downloaded your trunk code and the code isn't in the files you list it being in...sooooo I dunno where it is...

well it's even listed at the very end of this comment page (the last 5 lines register the functions)
are you sure got the right branch?

Revision history for this message
Travis (dinky-dye-aussie) wrote :

Yep, I downloaded from your trunk, several times, and each time I compiled and tried to run the game using you're script code, it wouldn't work, because the code wasn't there.

I'll try again :)

Revision history for this message
Travis (dinky-dye-aussie) wrote :

OK so now it works. I don't know what I did differently, but it works...How do I zoom tho? My native resolution is 1680x1050, and the game looks sick on this resolution...How do I zoom in though?

Revision history for this message
treba (der-treba) wrote :

> OK so now it works. I don't know what I did differently, but it works...How do
> I zoom tho? My native resolution is 1680x1050, and the game looks sick on this
> resolution...How do I zoom in though?

afaik that's possible with the other proposed merge. probably a good combination of merges =)

Revision history for this message
Pali (pali) wrote :

There is duplicate code for SDL video flags. One in function GetVideoFlags (src/video/video.cpp) and one in InitVideoSdl (src/video/sdl.cpp). There is also duplicate function call SDL_Init() in src/video/sdl.cpp

And please squash that two commits into one because second commit deleting code which was added by first. You can do that with command: bzr uncommit

review: Needs Fixing

Unmerged revisions

8698. By treba

- removed declarations of lua bindings from video.h
- replaced resolution calls with the following two:
 GetCurrentResolution() -- array with width and height of current resolution. if called at startup, the optimal resolution is given
 GetAvailableResolutions() -- two dimensional array with available resolutions. in window mode gives nil

8697. By treba

added lua queries to determine the default resolution of a screen aswell as queries to for supported resolutions.

GetVideoResolutionsNr() - get number of available resolutions
GetResolutionsWidth(i) - get width of resolution i
GetResolutionsHeight(i) - see above
GetCurrentResolutionsWidth() - get current resolution width. if queried on startup, it's the default/optimal resolution
GetCurrentResolutionsHeight() - see above

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/game/game.cpp'
2--- src/game/game.cpp 2013-04-23 13:45:52 +0000
3+++ src/game/game.cpp 2013-04-23 18:34:43 +0000
4@@ -1490,6 +1490,7 @@
5 UpgradesCclRegister();
6 UserInterfaceCclRegister();
7 VideoCclRegister();
8+ VideoModesRegister();
9 }
10
11
12
13=== modified file 'src/include/video.h'
14--- src/include/video.h 2013-04-13 00:57:39 +0000
15+++ src/include/video.h 2013-04-23 18:34:43 +0000
16@@ -400,9 +400,11 @@
17
18 /// register lua function
19 extern void VideoCclRegister();
20+extern void VideoModesRegister();
21
22 /// initialize the video part
23 extern void InitVideo();
24+extern void PreInitVideoSdl();
25
26 /// deinitliaize the video part
27 void DeInitVideo();
28
29=== modified file 'src/stratagus/stratagus.cpp'
30--- src/stratagus/stratagus.cpp 2013-04-14 08:42:04 +0000
31+++ src/stratagus/stratagus.cpp 2013-04-23 18:34:43 +0000
32@@ -649,6 +649,9 @@
33
34 makedir(parameters.GetUserDirectory().c_str(), 0777);
35
36+ // Preinit SDL_Video so video infos can be queried
37+ PreInitVideoSdl();
38+
39 // Init Lua and register lua functions!
40 InitLua();
41 LuaRegisterModules();
42
43=== modified file 'src/video/sdl.cpp'
44--- src/video/sdl.cpp 2013-04-13 00:57:39 +0000
45+++ src/video/sdl.cpp 2013-04-23 18:34:43 +0000
46@@ -453,17 +453,40 @@
47 }
48
49 /**
50+ * Preinit Sdl so sdl infos can be queried by lua
51+ */
52+
53+void PreInitVideoSdl()
54+{
55+ int res = SDL_Init(
56+#ifdef DEBUG
57+ SDL_INIT_NOPARACHUTE |
58+#endif
59+ SDL_INIT_AUDIO | SDL_INIT_VIDEO |
60+ SDL_INIT_TIMER);
61+ if (res < 0) {
62+ fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
63+ exit(1);
64+ }
65+
66+ // Clean up on exit
67+ atexit(SDL_Quit);
68+}
69+
70+/**
71 ** Initialize the video part for SDL.
72 */
73 void InitVideoSdl()
74 {
75 Uint32 flags = 0;
76
77+#ifndef USE_WIN32
78+ // Fix tablet input in full-screen mode
79+ SDL_putenv(strdup("SDL_MOUSE_RELATIVE=0"));
80+#endif
81+
82 if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
83-#ifndef USE_WIN32
84- // Fix tablet input in full-screen mode
85- SDL_putenv(strdup("SDL_MOUSE_RELATIVE=0"));
86-#endif
87+
88 int res = SDL_Init(
89 #ifdef DEBUG
90 SDL_INIT_NOPARACHUTE |
91@@ -477,106 +500,106 @@
92
93 // Clean up on exit
94 atexit(SDL_Quit);
95+ }
96
97 #ifdef USE_MAEMO
98- maemo_init();
99+ maemo_init();
100 #endif
101
102- // If debug is enabled, Stratagus disable SDL Parachute.
103- // So we need gracefully handle segfaults and aborts.
104+ // If debug is enabled, Stratagus disable SDL Parachute.
105+ // So we need gracefully handle segfaults and aborts.
106 #if defined(DEBUG) && !defined(USE_WIN32)
107- signal(SIGSEGV, CleanExit);
108- signal(SIGABRT, CleanExit);
109+ signal(SIGSEGV, CleanExit);
110+ signal(SIGABRT, CleanExit);
111 #endif
112- // Set WindowManager Title
113- if (!FullGameName.empty()) {
114- SDL_WM_SetCaption(FullGameName.c_str(), FullGameName.c_str());
115- } else if (!Parameters::Instance.applicationName.empty()) {
116- SDL_WM_SetCaption(Parameters::Instance.applicationName.c_str(), Parameters::Instance.applicationName.c_str());
117- } else {
118- SDL_WM_SetCaption("Stratagus", "Stratagus");
119- }
120+ // Set WindowManager Title
121+ if (!FullGameName.empty()) {
122+ SDL_WM_SetCaption(FullGameName.c_str(), FullGameName.c_str());
123+ } else if (!Parameters::Instance.applicationName.empty()) {
124+ SDL_WM_SetCaption(Parameters::Instance.applicationName.c_str(), Parameters::Instance.applicationName.c_str());
125+ } else {
126+ SDL_WM_SetCaption("Stratagus", "Stratagus");
127+ }
128
129 #if ! defined(USE_WIN32) && ! defined(USE_MAEMO)
130
131 #if defined(USE_OPENGL) || defined(USE_GLES)
132- // Make sure, that we not create OpenGL textures (and do not call OpenGL functions), when creating icon surface
133- bool UseOpenGL_orig = UseOpenGL;
134- UseOpenGL = false;
135+ // Make sure, that we not create OpenGL textures (and do not call OpenGL functions), when creating icon surface
136+ bool UseOpenGL_orig = UseOpenGL;
137+ UseOpenGL = false;
138 #endif
139
140- SDL_Surface *icon = NULL;
141- CGraphic *g = NULL;
142- struct stat st;
143-
144- std::string FullGameNameL = FullGameName;
145- for (size_t i = 0; i < FullGameNameL.size(); ++i) {
146- FullGameNameL[i] = tolower(FullGameNameL[i]);
147- }
148-
149- std::string ApplicationName = Parameters::Instance.applicationName;
150- std::string ApplicationNameL = ApplicationName;
151- for (size_t i = 0; i < ApplicationNameL.size(); ++i) {
152- ApplicationNameL[i] = tolower(ApplicationNameL[i]);
153- }
154-
155- std::vector <std::string> pixmaps;
156- pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameName + ".png");
157- pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameNameL + ".png");
158- pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameName + ".png");
159- pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameNameL + ".png");
160- pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationName + ".png");
161- pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationNameL + ".png");
162- pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationName + ".png");
163- pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationNameL + ".png");
164- pixmaps.push_back(std::string() + PIXMAPS + "/" + "Stratagus" + ".png");
165- pixmaps.push_back(std::string() + PIXMAPS + "/" + "stratagus" + ".png");
166- pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "Stratagus" + ".png");
167- pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "stratagus" + ".png");
168-
169- for (size_t i = 0; i < pixmaps.size(); ++i) {
170- if (stat(pixmaps[i].c_str(), &st) == 0) {
171- if (g) { CGraphic::Free(g); }
172- g = CGraphic::New(pixmaps[i].c_str());
173- g->Load();
174- icon = g->Surface;
175- if (icon) { break; }
176- }
177- }
178-
179- if (icon) {
180- SDL_WM_SetIcon(icon, 0);
181- }
182-
183- if (g) {
184- CGraphic::Free(g);
185- }
186+ SDL_Surface *icon = NULL;
187+ CGraphic *g = NULL;
188+ struct stat st;
189+
190+ std::string FullGameNameL = FullGameName;
191+ for (size_t i = 0; i < FullGameNameL.size(); ++i) {
192+ FullGameNameL[i] = tolower(FullGameNameL[i]);
193+ }
194+
195+ std::string ApplicationName = Parameters::Instance.applicationName;
196+ std::string ApplicationNameL = ApplicationName;
197+ for (size_t i = 0; i < ApplicationNameL.size(); ++i) {
198+ ApplicationNameL[i] = tolower(ApplicationNameL[i]);
199+ }
200+
201+ std::vector <std::string> pixmaps;
202+ pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameName + ".png");
203+ pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameNameL + ".png");
204+ pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameName + ".png");
205+ pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameNameL + ".png");
206+ pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationName + ".png");
207+ pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationNameL + ".png");
208+ pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationName + ".png");
209+ pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationNameL + ".png");
210+ pixmaps.push_back(std::string() + PIXMAPS + "/" + "Stratagus" + ".png");
211+ pixmaps.push_back(std::string() + PIXMAPS + "/" + "stratagus" + ".png");
212+ pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "Stratagus" + ".png");
213+ pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "stratagus" + ".png");
214+
215+ for (size_t i = 0; i < pixmaps.size(); ++i) {
216+ if (stat(pixmaps[i].c_str(), &st) == 0) {
217+ if (g) { CGraphic::Free(g); }
218+ g = CGraphic::New(pixmaps[i].c_str());
219+ g->Load();
220+ icon = g->Surface;
221+ if (icon) { break; }
222+ }
223+ }
224+
225+ if (icon) {
226+ SDL_WM_SetIcon(icon, 0);
227+ }
228+
229+ if (g) {
230+ CGraphic::Free(g);
231+ }
232
233 #if defined(USE_OPENGL) || defined(USE_GLES)
234- UseOpenGL = UseOpenGL_orig;
235+ UseOpenGL = UseOpenGL_orig;
236 #endif
237
238 #endif
239 #ifdef USE_WIN32
240- HWND hwnd = NULL;
241- HICON hicon = NULL;
242- SDL_SysWMinfo info;
243- SDL_VERSION(&info.version);
244-
245- if (SDL_GetWMInfo(&info)) {
246- hwnd = info.window;
247- }
248-
249- if (hwnd) {
250- hicon = ExtractIcon(GetModuleHandle(NULL), Parameters::Instance.applicationName.c_str(), 0);
251- }
252-
253- if (hicon) {
254- SendMessage(hwnd, (UINT)WM_SETICON, ICON_SMALL, (LPARAM)hicon);
255- SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon);
256- }
257+ HWND hwnd = NULL;
258+ HICON hicon = NULL;
259+ SDL_SysWMinfo info;
260+ SDL_VERSION(&info.version);
261+
262+ if (SDL_GetWMInfo(&info)) {
263+ hwnd = info.window;
264+ }
265+
266+ if (hwnd) {
267+ hicon = ExtractIcon(GetModuleHandle(NULL), Parameters::Instance.applicationName.c_str(), 0);
268+ }
269+
270+ if (hicon) {
271+ SendMessage(hwnd, (UINT)WM_SETICON, ICON_SMALL, (LPARAM)hicon);
272+ SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon);
273+ }
274 #endif
275- }
276
277 // Initialize the display
278
279
280=== modified file 'src/video/video.cpp'
281--- src/video/video.cpp 2013-04-13 00:57:39 +0000
282+++ src/video/video.cpp 2013-04-23 18:34:43 +0000
283@@ -480,3 +480,77 @@
284 #endif
285
286 //@}
287+
288+/*
289+ * Lua queries for video information
290+ */
291+
292+static Uint32 GetVideoFlags(){
293+ Uint32 flags = 0;
294+
295+#if !defined(USE_OPENGL) && !defined(USE_GLES)
296+ flags = SDL_HWSURFACE | SDL_HWPALETTE;
297+#endif
298+
299+ if (Video.FullScreen) {
300+ flags |= SDL_FULLSCREEN;
301+ }
302+
303+#if defined(USE_OPENGL) || defined(USE_GLES)
304+#ifdef USE_GLES_NATIVE
305+ flags |= SDL_OPENGLES;
306+#endif
307+#ifdef USE_GLES_MAEMO
308+ flags |= SDL_SWSURFACE;
309+#endif
310+#ifdef USE_OPENGL
311+ flags |= SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
312+#endif
313+#endif
314+
315+ return flags;
316+}
317+
318+int GetAvailableResolutions(lua_State *l)
319+{
320+ SDL_Rect** modes = SDL_ListModes(NULL, GetVideoFlags());
321+ if (modes == (SDL_Rect**)-1 || modes == (SDL_Rect**)0) {
322+ lua_pushnil(l);
323+ }
324+ else {
325+ lua_newtable(l);
326+ int mainTable = lua_gettop(l);
327+ int index = 0;
328+
329+ for(int i=0;modes[i];++i){
330+ lua_newtable(l);
331+ int subTable = lua_gettop(l);
332+ lua_pushnumber(l, lua_Number(modes[i]->w));
333+ lua_rawseti(l, subTable, 1);
334+ lua_pushnumber(l, lua_Number(modes[i]->h));
335+ lua_rawseti(l, subTable, 2);
336+
337+ lua_rawseti(l, mainTable, ++index);
338+ }
339+ }
340+
341+ return 1;
342+}
343+
344+int GetCurrentResolution(lua_State *l)
345+{
346+ lua_newtable(l);
347+ int mainTable = lua_gettop(l);
348+ lua_pushnumber(l, lua_Number(SDL_GetVideoInfo()->current_w));
349+ lua_rawseti(l, mainTable, 1);
350+ lua_pushnumber(l, lua_Number(SDL_GetVideoInfo()->current_h));
351+ lua_rawseti(l, mainTable, 2);
352+
353+ return 1;
354+}
355+
356+void VideoModesRegister()
357+{
358+ lua_register(Lua, "GetAvailableResolutions", GetAvailableResolutions);
359+ lua_register(Lua, "GetCurrentResolution", GetCurrentResolution);
360+}

Subscribers

People subscribed via source and target branches

to status/vote changes: