/// Get the dimensions of a camera on the actual // screen. It usually fills the whole window (= the full size of the // resolution you specified in @{blitwizard.graphics.setMode}), // but that may be changed with // @{blitwizard.graphics.camera:setPixelDimensionsOnScreen| // camera:setPixelDimensionsOnScreen}. // @function getPixelDimensionsOnScreen // @treturn number width Width of the camera on the screen in pixels // @treturn number height Height of the camera on the screen in pixels int luafuncs_camera_getPixelDimensionsOnScreen(lua_State* l) { struct luacameralistentry* e = toluacameralistentry( l, 1, 0, "blitwizard.graphics.camera:getPixelDimensionsOnScreen"); lua_pushnumber(l, graphics_getCameraWidth(e->cameraslot)); lua_pushnumber(l, graphics_getCameraHeight(e->cameraslot)); return 2; }
/// Get the size of the camera in game units (so the size of the // visible world area it shows). // // consider_zoom = false gives you the area of // @{blitwizard.object:pinToCamera|pinned objects} which the camera // shows, which is not influenced by zoom. (this is the default if // consider_zoom is no specified at all) // // consider_zoom = true is a special case useful for 2d, // which gives you the camera's dimensions according to the current // zoom factor. // // The result of this function depends on the camera's // @{blitwizard.graphics.camera:getPixelDimensionsOnScreen| // actual size in pixels on the screen}, the camera's // @{blitwizard.graphics.camera:get2dZoomFactor| // 2d zoom factor} (for consider_zoom set to true) and the camera's // @{blitwizard.graphics.camera:get2dAspectRatio| // 2d aspect ratio}. // // <i>Note on @{blitwizard.object:pinToCamera|pinned} 2d objects:</i> // If you want to know the area visible in game units for // @{blitwizard.object:pinToCamera|pinned objects} which are unaffected // by camera zoom when drawn, specify <i>false</i> as first parameter // for not taking the zoom into account. // @function getDimensions // @tparam boolean consider_zoom (optional) defaults to false. // Specifies whether the 2d zoom factor should be taken into account. // Set to <i>true</i> if yes, <i>false</i> if not. // @treturn number width the width of the camera in game units // @treturn number height the height of the camera in game units int luafuncs_camera_getDimensions(lua_State* l) { struct luacameralistentry* e = toluacameralistentry( l, 1, 0, "blitwizard.graphics.camera:getDimensions"); // if the camera default unit -> pixel conversion size isn't known yet: if (!unittopixelsset) { return haveluaerror(l, "this function is unavailable before the " "first blitwizard.graphics.setMode call"); } int considerzoom = 0; // get consider_zoom parameter: if (lua_gettop(l) >= 2 && lua_type(l, 2) != LUA_TNIL) { if (lua_type(l, 2) != LUA_TBOOLEAN) { return haveluaerror(l, badargument1, 1, "blitwizard.graphics.camera:" "getDimensions", "boolean", lua_strtype(l, 2)); } considerzoom = lua_toboolean(l, 2); } // return visible area dimensions: // FIXME: take aspect ratio into account once it can be changed double w = graphics_getCameraWidth(e->cameraslot); double h = graphics_getCameraHeight(e->cameraslot); double z = graphics_getCamera2DZoom(e->cameraslot); if (!considerzoom) { z = 1; } lua_pushnumber(l, (w/UNIT_TO_PIXELS) * z); lua_pushnumber(l, (h/UNIT_TO_PIXELS) * z); return 2; }
/// Specify a 2d position in the range from 0,0 to // w,h with w,h being the camera visible area size // as from @{blitwizard.graphics.camera:getDimensions| // getDimensions} with consider_zoom set to <b>false</b>. // // (So the position you specify is the coordinates a // @{blitwizard.object:pinToCamera|pinned object} would have on screen // or the coordinates you get from @{blitwizard.onMouseMove}) // // The position can also be smaller than 0,0 or larger than the // actual camera dimensions, in which case you'll get an out-of-screen // world position as a result. // @function screenPosTo2dWorldPos // @tparam number pos_x the X coordinate of the screen position // @tparam number pos_y the Y coordinate of the screen position // @tparam number parallax (optional) if you want, specify the @{blitwizard.object:setParallax|parallax} effect strength to get the world position displaced accordingly for an object with that parallax effect strength // @treturn number X coordinate of the resulting world position // @treturn number Y coordinate of the resulting world position int luafuncs_camera_screenPosTo2dWorldPos(lua_State* l) { struct luacameralistentry* e = toluacameralistentry( l, 1, 0, "blitwizard.graphics.camera:screenPosTo2dWorldPos"); if (lua_type(l, 2) != LUA_TNUMBER) { return haveluaerror(l, badargument1, 1, "blitwizard.graphics.camera:screenPosTo2dWorldPos", "number", lua_strtype(l, 2)); } if (lua_type(l, 3) != LUA_TNUMBER) { return haveluaerror(l, badargument1, 2, "blitwizard.graphics.camera:screenPosTo2dWorldPos", "number", lua_strtype(l, 3)); } double parallax = 1; if (lua_gettop(l) >= 4 && lua_type(l, 4) != LUA_TNIL) { if (lua_type(l, 4) != LUA_TNUMBER) { return haveluaerror(l, badargument1, 3, "blitwizard.graphics.camera:screenPosTo2dWorldPos", "number", lua_strtype(l, 4)); } if (lua_tonumber(l, 4) <= 0) { return haveluaerror(l, badargument2, 3, "blitwizard.graphics.camera:screenPosTo2dWorldPos", "parallax effect strength needs to be greater than zero"); } parallax = lua_tonumber(l, 4); } double x = lua_tonumber(l, 2); double y = lua_tonumber(l, 3); // calculate camera top left world position: double tx = graphics_getCamera2DCenterX(e->cameraslot); double ty = graphics_getCamera2DCenterY(e->cameraslot); double zoomscale = (UNIT_TO_PIXELS * graphics_getCamera2DZoom(e->cameraslot)) / (double)UNIT_TO_PIXELS_DEFAULT; double cameraWidth = graphics_getCameraWidth(e->cameraslot) / (double)UNIT_TO_PIXELS; double cameraHeight = graphics_getCameraHeight(e->cameraslot) / (double)UNIT_TO_PIXELS; tx -= (cameraWidth / zoomscale) * 0.5f; ty -= (cameraHeight / zoomscale) * 0.5f; // apply parallax effect as desired with screen center as parallax center: x -= cameraWidth / 2; y -= cameraHeight / 2; x /= parallax; y /= parallax; x += cameraWidth / 2; y += cameraHeight / 2; // the onscreen coordinates need to be translated into "zoomed" space: x /= graphics_getCamera2DZoom(e->cameraslot); y /= graphics_getCamera2DZoom(e->cameraslot); lua_pushnumber(l, tx + x); lua_pushnumber(l, ty + y); return 2; }
void graphicsrender_draw(void) { graphicssdlrender_startFrame(); graphics2dsprites_lockListOrTreeAccess(); // render world sprites: double w = graphics_getCameraWidth(0); double h = graphics_getCameraHeight(0); double z = graphics_getCamera2DZoom(0); double cwidth = (w/UNIT_TO_PIXELS) * z; double cheight = (w/UNIT_TO_PIXELS) * z; double ctopleftx = graphics_getCamera2DCenterX(0) - cwidth/2; double ctoplefty = graphics_getCamera2DCenterY(0) - cheight/2; graphics2dspritestree_doForAllSpritesSortedBottomToTop( ctopleftx, ctoplefty, cwidth, cheight, &graphicssdlrender_spriteCallback, NULL); // render camera-pinned sprites: graphics2dspriteslist_doForAllSpritesBottomToTop( &graphicssdlrender_spriteCallback, NULL); graphics2dsprites_releaseListOrTreeAccess(); graphicssdlrender_completeFrame(); }