Window and Background Image setup

Here’s a snippet of code for prepping a games window, setting the resolution and making it fullscreen, and also for creating a tiled background image to cover it.

It assumes that the screen is wider than it is tall, which I’m pretty sure is a safe assumption. The “image” it loads should be a square texture card 2 units to a side created with egg-texture-cards, using -g -1,1,-1,1.

The script appropriates base.aspect2dp, which is unused by panda3d, and forces it to render in the back with a sortOrder call. To aspect2dp it attaches a series of copies of the background image to ensure total coverage of the screen, and then uses clearModelNodes and flattenStrong to condense the images down to a single geom.

from pandac.PandaModules import *

winProps = WindowProperties()
winProps.setSize(1024, 768)
winProps.setFullscreen(True)
base.win.requestProperties(winProps)

widthMult = winProps.getXSize() / float(winProps.getYSize())
imageQuantity = int(widthMult // 1)
if(widthMult % 1 > 0):
  imageQuantity += 1
bgParent = base.aspect2dp.attachNewNode("Background Parent")
if(imageQuantity % 2 == 0):
  for nA in range(imageQuantity):
    image = loader.loadModel("Images/Screens/Background")
    image.reparentTo(bgParent)
    if(nA % 2 == 0):
      image.setPos(nA * 2 + 1, 0, 0)
    else:
      image.setPos((nA-1) * -2 - 1, 0, 0)
else:
  for nA in range(imageQuantity):
    image = loader.loadModel("Images/Screens/Background")
    image.reparentTo(bgParent)
    if(nA != 0):
      if(nA % 2 == 0):
        image.setPos((nA-1) * 2, 0, 0)
      else:
        image.setPos(nA * -2, 0, 0)
bgParent.clearModelNodes()
bgParent.flattenStrong()
base.cam2dp.node().getDisplayRegion(0).setSort(-20)