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
=== modified file 'src/game/game.cpp'
--- src/game/game.cpp 2013-04-23 13:45:52 +0000
+++ src/game/game.cpp 2013-04-23 18:34:43 +0000
@@ -1490,6 +1490,7 @@
1490 UpgradesCclRegister();1490 UpgradesCclRegister();
1491 UserInterfaceCclRegister();1491 UserInterfaceCclRegister();
1492 VideoCclRegister();1492 VideoCclRegister();
1493 VideoModesRegister();
1493}1494}
14941495
14951496
14961497
=== modified file 'src/include/video.h'
--- src/include/video.h 2013-04-13 00:57:39 +0000
+++ src/include/video.h 2013-04-23 18:34:43 +0000
@@ -400,9 +400,11 @@
400400
401/// register lua function401/// register lua function
402extern void VideoCclRegister();402extern void VideoCclRegister();
403extern void VideoModesRegister();
403404
404/// initialize the video part405/// initialize the video part
405extern void InitVideo();406extern void InitVideo();
407extern void PreInitVideoSdl();
406408
407/// deinitliaize the video part409/// deinitliaize the video part
408void DeInitVideo();410void DeInitVideo();
409411
=== modified file 'src/stratagus/stratagus.cpp'
--- src/stratagus/stratagus.cpp 2013-04-14 08:42:04 +0000
+++ src/stratagus/stratagus.cpp 2013-04-23 18:34:43 +0000
@@ -649,6 +649,9 @@
649649
650 makedir(parameters.GetUserDirectory().c_str(), 0777);650 makedir(parameters.GetUserDirectory().c_str(), 0777);
651651
652 // Preinit SDL_Video so video infos can be queried
653 PreInitVideoSdl();
654
652 // Init Lua and register lua functions!655 // Init Lua and register lua functions!
653 InitLua();656 InitLua();
654 LuaRegisterModules();657 LuaRegisterModules();
655658
=== modified file 'src/video/sdl.cpp'
--- src/video/sdl.cpp 2013-04-13 00:57:39 +0000
+++ src/video/sdl.cpp 2013-04-23 18:34:43 +0000
@@ -453,17 +453,40 @@
453}453}
454454
455/**455/**
456 * Preinit Sdl so sdl infos can be queried by lua
457 */
458
459void PreInitVideoSdl()
460{
461 int res = SDL_Init(
462#ifdef DEBUG
463 SDL_INIT_NOPARACHUTE |
464#endif
465 SDL_INIT_AUDIO | SDL_INIT_VIDEO |
466 SDL_INIT_TIMER);
467 if (res < 0) {
468 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
469 exit(1);
470 }
471
472 // Clean up on exit
473 atexit(SDL_Quit);
474}
475
476/**
456** Initialize the video part for SDL.477** Initialize the video part for SDL.
457*/478*/
458void InitVideoSdl()479void InitVideoSdl()
459{480{
460 Uint32 flags = 0;481 Uint32 flags = 0;
461482
483#ifndef USE_WIN32
484 // Fix tablet input in full-screen mode
485 SDL_putenv(strdup("SDL_MOUSE_RELATIVE=0"));
486#endif
487
462 if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {488 if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
463#ifndef USE_WIN32489
464 // Fix tablet input in full-screen mode
465 SDL_putenv(strdup("SDL_MOUSE_RELATIVE=0"));
466#endif
467 int res = SDL_Init(490 int res = SDL_Init(
468#ifdef DEBUG491#ifdef DEBUG
469 SDL_INIT_NOPARACHUTE |492 SDL_INIT_NOPARACHUTE |
@@ -477,106 +500,106 @@
477500
478 // Clean up on exit501 // Clean up on exit
479 atexit(SDL_Quit);502 atexit(SDL_Quit);
503 }
480504
481#ifdef USE_MAEMO505#ifdef USE_MAEMO
482 maemo_init();506 maemo_init();
483#endif507#endif
484508
485 // If debug is enabled, Stratagus disable SDL Parachute.509 // If debug is enabled, Stratagus disable SDL Parachute.
486 // So we need gracefully handle segfaults and aborts.510 // So we need gracefully handle segfaults and aborts.
487#if defined(DEBUG) && !defined(USE_WIN32)511#if defined(DEBUG) && !defined(USE_WIN32)
488 signal(SIGSEGV, CleanExit);512 signal(SIGSEGV, CleanExit);
489 signal(SIGABRT, CleanExit);513 signal(SIGABRT, CleanExit);
490#endif514#endif
491 // Set WindowManager Title515 // Set WindowManager Title
492 if (!FullGameName.empty()) {516 if (!FullGameName.empty()) {
493 SDL_WM_SetCaption(FullGameName.c_str(), FullGameName.c_str());517 SDL_WM_SetCaption(FullGameName.c_str(), FullGameName.c_str());
494 } else if (!Parameters::Instance.applicationName.empty()) {518 } else if (!Parameters::Instance.applicationName.empty()) {
495 SDL_WM_SetCaption(Parameters::Instance.applicationName.c_str(), Parameters::Instance.applicationName.c_str());519 SDL_WM_SetCaption(Parameters::Instance.applicationName.c_str(), Parameters::Instance.applicationName.c_str());
496 } else {520 } else {
497 SDL_WM_SetCaption("Stratagus", "Stratagus");521 SDL_WM_SetCaption("Stratagus", "Stratagus");
498 }522 }
499523
500#if ! defined(USE_WIN32) && ! defined(USE_MAEMO)524#if ! defined(USE_WIN32) && ! defined(USE_MAEMO)
501525
502#if defined(USE_OPENGL) || defined(USE_GLES)526#if defined(USE_OPENGL) || defined(USE_GLES)
503 // Make sure, that we not create OpenGL textures (and do not call OpenGL functions), when creating icon surface527 // Make sure, that we not create OpenGL textures (and do not call OpenGL functions), when creating icon surface
504 bool UseOpenGL_orig = UseOpenGL;528 bool UseOpenGL_orig = UseOpenGL;
505 UseOpenGL = false;529 UseOpenGL = false;
506#endif530#endif
507531
508 SDL_Surface *icon = NULL;532 SDL_Surface *icon = NULL;
509 CGraphic *g = NULL;533 CGraphic *g = NULL;
510 struct stat st;534 struct stat st;
511535
512 std::string FullGameNameL = FullGameName;536 std::string FullGameNameL = FullGameName;
513 for (size_t i = 0; i < FullGameNameL.size(); ++i) {537 for (size_t i = 0; i < FullGameNameL.size(); ++i) {
514 FullGameNameL[i] = tolower(FullGameNameL[i]);538 FullGameNameL[i] = tolower(FullGameNameL[i]);
515 }539 }
516540
517 std::string ApplicationName = Parameters::Instance.applicationName;541 std::string ApplicationName = Parameters::Instance.applicationName;
518 std::string ApplicationNameL = ApplicationName;542 std::string ApplicationNameL = ApplicationName;
519 for (size_t i = 0; i < ApplicationNameL.size(); ++i) {543 for (size_t i = 0; i < ApplicationNameL.size(); ++i) {
520 ApplicationNameL[i] = tolower(ApplicationNameL[i]);544 ApplicationNameL[i] = tolower(ApplicationNameL[i]);
521 }545 }
522546
523 std::vector <std::string> pixmaps;547 std::vector <std::string> pixmaps;
524 pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameName + ".png");548 pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameName + ".png");
525 pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameNameL + ".png");549 pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameNameL + ".png");
526 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameName + ".png");550 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameName + ".png");
527 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameNameL + ".png");551 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameNameL + ".png");
528 pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationName + ".png");552 pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationName + ".png");
529 pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationNameL + ".png");553 pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationNameL + ".png");
530 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationName + ".png");554 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationName + ".png");
531 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationNameL + ".png");555 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationNameL + ".png");
532 pixmaps.push_back(std::string() + PIXMAPS + "/" + "Stratagus" + ".png");556 pixmaps.push_back(std::string() + PIXMAPS + "/" + "Stratagus" + ".png");
533 pixmaps.push_back(std::string() + PIXMAPS + "/" + "stratagus" + ".png");557 pixmaps.push_back(std::string() + PIXMAPS + "/" + "stratagus" + ".png");
534 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "Stratagus" + ".png");558 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "Stratagus" + ".png");
535 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "stratagus" + ".png");559 pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "stratagus" + ".png");
536560
537 for (size_t i = 0; i < pixmaps.size(); ++i) {561 for (size_t i = 0; i < pixmaps.size(); ++i) {
538 if (stat(pixmaps[i].c_str(), &st) == 0) {562 if (stat(pixmaps[i].c_str(), &st) == 0) {
539 if (g) { CGraphic::Free(g); }563 if (g) { CGraphic::Free(g); }
540 g = CGraphic::New(pixmaps[i].c_str());564 g = CGraphic::New(pixmaps[i].c_str());
541 g->Load();565 g->Load();
542 icon = g->Surface;566 icon = g->Surface;
543 if (icon) { break; }567 if (icon) { break; }
544 }568 }
545 }569 }
546570
547 if (icon) {571 if (icon) {
548 SDL_WM_SetIcon(icon, 0);572 SDL_WM_SetIcon(icon, 0);
549 }573 }
550574
551 if (g) {575 if (g) {
552 CGraphic::Free(g);576 CGraphic::Free(g);
553 }577 }
554578
555#if defined(USE_OPENGL) || defined(USE_GLES)579#if defined(USE_OPENGL) || defined(USE_GLES)
556 UseOpenGL = UseOpenGL_orig;580 UseOpenGL = UseOpenGL_orig;
557#endif581#endif
558582
559#endif583#endif
560#ifdef USE_WIN32584#ifdef USE_WIN32
561 HWND hwnd = NULL;585 HWND hwnd = NULL;
562 HICON hicon = NULL;586 HICON hicon = NULL;
563 SDL_SysWMinfo info;587 SDL_SysWMinfo info;
564 SDL_VERSION(&info.version);588 SDL_VERSION(&info.version);
565589
566 if (SDL_GetWMInfo(&info)) {590 if (SDL_GetWMInfo(&info)) {
567 hwnd = info.window;591 hwnd = info.window;
568 }592 }
569593
570 if (hwnd) {594 if (hwnd) {
571 hicon = ExtractIcon(GetModuleHandle(NULL), Parameters::Instance.applicationName.c_str(), 0);595 hicon = ExtractIcon(GetModuleHandle(NULL), Parameters::Instance.applicationName.c_str(), 0);
572 }596 }
573597
574 if (hicon) {598 if (hicon) {
575 SendMessage(hwnd, (UINT)WM_SETICON, ICON_SMALL, (LPARAM)hicon);599 SendMessage(hwnd, (UINT)WM_SETICON, ICON_SMALL, (LPARAM)hicon);
576 SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon);600 SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon);
577 }601 }
578#endif602#endif
579 }
580603
581 // Initialize the display604 // Initialize the display
582605
583606
=== modified file 'src/video/video.cpp'
--- src/video/video.cpp 2013-04-13 00:57:39 +0000
+++ src/video/video.cpp 2013-04-23 18:34:43 +0000
@@ -480,3 +480,77 @@
480#endif480#endif
481481
482//@}482//@}
483
484/*
485 * Lua queries for video information
486 */
487
488static Uint32 GetVideoFlags(){
489 Uint32 flags = 0;
490
491#if !defined(USE_OPENGL) && !defined(USE_GLES)
492 flags = SDL_HWSURFACE | SDL_HWPALETTE;
493#endif
494
495 if (Video.FullScreen) {
496 flags |= SDL_FULLSCREEN;
497 }
498
499#if defined(USE_OPENGL) || defined(USE_GLES)
500#ifdef USE_GLES_NATIVE
501 flags |= SDL_OPENGLES;
502#endif
503#ifdef USE_GLES_MAEMO
504 flags |= SDL_SWSURFACE;
505#endif
506#ifdef USE_OPENGL
507 flags |= SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
508#endif
509#endif
510
511 return flags;
512}
513
514int GetAvailableResolutions(lua_State *l)
515{
516 SDL_Rect** modes = SDL_ListModes(NULL, GetVideoFlags());
517 if (modes == (SDL_Rect**)-1 || modes == (SDL_Rect**)0) {
518 lua_pushnil(l);
519 }
520 else {
521 lua_newtable(l);
522 int mainTable = lua_gettop(l);
523 int index = 0;
524
525 for(int i=0;modes[i];++i){
526 lua_newtable(l);
527 int subTable = lua_gettop(l);
528 lua_pushnumber(l, lua_Number(modes[i]->w));
529 lua_rawseti(l, subTable, 1);
530 lua_pushnumber(l, lua_Number(modes[i]->h));
531 lua_rawseti(l, subTable, 2);
532
533 lua_rawseti(l, mainTable, ++index);
534 }
535 }
536
537 return 1;
538}
539
540int GetCurrentResolution(lua_State *l)
541{
542 lua_newtable(l);
543 int mainTable = lua_gettop(l);
544 lua_pushnumber(l, lua_Number(SDL_GetVideoInfo()->current_w));
545 lua_rawseti(l, mainTable, 1);
546 lua_pushnumber(l, lua_Number(SDL_GetVideoInfo()->current_h));
547 lua_rawseti(l, mainTable, 2);
548
549 return 1;
550}
551
552void VideoModesRegister()
553{
554 lua_register(Lua, "GetAvailableResolutions", GetAvailableResolutions);
555 lua_register(Lua, "GetCurrentResolution", GetCurrentResolution);
556}

Subscribers

People subscribed via source and target branches

to status/vote changes: