0

Im trying to add background to my pygame/opengl game. But I cannot do it and i do not understand why.

I tried doing this

bg_img = pygame.image.load('./textures/background_image.jpg')
bg_img = pygame.transform.scale(bg_img, display)
window.blit(bg_img, (0, 0))

but it doesnt work, and i dont know why.

    def draw_map(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()

        glTranslatef(self.xtrans, self.ytrans, -5.0 * self.zoom)
        glRotatef(self.xrot, 1.0, 0.0, 0.0)
        glRotatef(self.yrot, 0.0, 1.0, 0.0)

        glBegin(GL_LINES)
        for point in self.tree.map:
            for i, connection in enumerate(point.connections):
                glColor3f(1.0, 1.0, 1.0)
                glVertex3f(point.x, point.y, point.z)
                glVertex3f(self.tree.map[connection].x, self.tree.map[connection].y, self.tree.map[connection].z)
        glEnd()

        glPointSize(10)
        glBegin(GL_POINTS)
        for agent in self.agents:
            glColor3f(1.0, 0.0, 0.0)
            position = self.tree.map[agent.position]
            glVertex3f(position.x, position.y, position.z)
        glEnd()

        glPointSize(20)
        glBegin(GL_POINTS)
        for point in self.tree.map:
            glColor3f(1.0, 1.0, 1.0)
            glVertex3f(point.x, point.y, point.z)
        glEnd()

        pygame.display.flip()



    def run(self):
        pygame.init()
        display = (800, 600)
        window = pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
        pygame.display.set_caption("Geco")


        glMatrixMode(GL_PROJECTION)
        gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)

        glMatrixMode(GL_MODELVIEW)
        glEnable(GL_DEPTH_TEST)

        clock = pygame.time.Clock()

        while True:
            self.handle_events()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()


            bg_img = pygame.image.load('./textures/background_image.jpg')
            bg_img = pygame.transform.scale(bg_img, display)
            window.blit(bg_img, (0, 0))

            self.draw_map()
            clock.tick(60)

Here is the code. As far as i understand problem can be that opengl is rendering window, not pygame, so pygame functions wont affect anything. Im not sure how to fix it. Thank you for helping in advance.

TRZN
  • 3
  • 3
  • You cannot use `blit` if you have an OpenGL window. You need to draw everything with OpenGL. See [How can I draw using pygame, while also drawing with pyopengl?](https://stackoverflow.com/questions/66552579/how-can-i-draw-using-pygame-while-also-drawing-with-pyopengl/66552664#66552664) – Rabbid76 May 18 '23 at 20:01
  • also see [pygame + opengl = display text](https://stackoverflow.com/questions/67608968/pygame-opengl-display-text) – Rabbid76 May 18 '23 at 20:09

0 Answers0