Search code examples
openglpygamepyopenglwindow-resize

Weird lines show up when changing the viewpoint on pyopenGL and pygame


so I am working on a 2d game using pygame and PyopenGL, I have done some stuff to make it so the window is resizable. When the window resizes, the game resizes with it. This is all good.

My game is a tile based game with scrolling up and down.

However, the issue is, when I resize the screen, if I scroll up or down, strange lines appear between the tiles. This doesnt happen when scrolling left or right, which is odd. These lines only appear when at higher resolutions, on lower resolutions they do not exist. My code is rather large and in multiple files, so I will just post a link to it on github, but here is my window code with the resize function:

import numpy

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

from . import batch
from . import GAH


class window_handler:
    def __init__(self,window_size=(640,360)):
        self.window_size = window_size
        self.main_window = pygame.display.set_mode(self.window_size, DOUBLEBUF|OPENGL|RESIZABLE)
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glViewport(0, 0, self.window_size[0], self.window_size[1])
        
        self.projection_matrix = numpy.array((
                (2/640, 0, 0, 0),
                (0, -2/360, 0, 0),
                (0, 0, 1, 0),
                (0, 0, 0, 1)
            ), numpy.float32)

        self.view_matrix = numpy.array((
                (1, 0, 0, 0),
                (0, 1, 0, 0),
                (0, 0, 1, 0),
                (-320, -180, 0, 1)
            ), numpy.float32)

        self.view_projection_matrix = numpy.dot(self.view_matrix,self.projection_matrix)
        batch.setup_shaders(vpMatrix = self.view_projection_matrix)
        self.renderer = batch.Batch()

    def rezise_request(self, event):
        self.window_size = event.w,event.h
        glViewport(0, 0, self.window_size[0], self.window_size[1])
        
    def update_display(self):
        pygame.display.flip()

    def draw_game(self,draw):
        glClearColor(0, 0, 0, 1)
        glClear(GL_COLOR_BUFFER_BIT)

        self.renderer.begin()
        
        for i in draw["background_0"]:
            self.renderer.draw(self.graphics_handler["Background_images"][i[1]][0],i[0][0],i[0][1])
        for i in draw["background_1"]:
            self.renderer.draw(self.graphics_handler["Background_images"][i[1]][0],i[0][0],i[0][1])
        for i in draw["background_2"]:
            self.renderer.draw(self.graphics_handler["Background_images"][i[1]][0],i[0][0],i[0][1])
        for i in draw["background_3"]:
            self.renderer.draw(self.graphics_handler["Background_images"][i[1]][0],i[0][0],i[0][1])

        for i in draw["tile_layer"]:
            self.renderer.draw(self.graphics_handler["Tileset"]["tileset_"+str(i[1][1])][i[1][0]],i[0][0],i[0][1])

        for i in draw["entity"].values():
            if i[1] != None:
                image = self.graphics_handler["Sprites"][i[1]][0]
                
                self.renderer.draw(image,i[0][0],i[0][1],i[2])
        self.renderer.end()

    def quit(self):
        self.renderer.delete()
        

        
        

Here is a a link to my code in full: github.com/EvelynMitchell199/Yumi


Solution

  • I figured out whats wrong with our code! So, basically when we set resolutions, if the resolution isn't one of the ones highlighted in green here, then everything scales weirdly creating lines between tiles. https://pacoup.com/2011/06/12/list-of-true-169-resolutions/

    The resolution MUST be divisible by 8 otherwise the scaling between tiles will be different, and thus we can get lines between tiles.