devkitpro libctru 3ds moving sprite is lagging

135 views Asked by At

I am creating a game for my nintendo 3ds. Its a platformer.

There I use sprite which are store in simple arrays and also some palettes. I am writing these Sprites directly into the framebuffer (so no citr02d). I am also using double buffering.

The problem is, that whenever I move a Sprite around the screen it lags and gets pressed together.

I've tried serval things to solve the problem like drawing the scene only every second frame, which worked but dimmed the whole screen.

Here is where I write to the buffer and draw the sprites:

void Screen::setPixel(sint x, sint y, usint color){

int pos = 2 * (height - 1 - y + x * height);    

if(pos >= 0 && pos < 191999){
    
  map[pos + 0] = color;
  map[pos + 1] = (color & 0b1111111100000000) >> 8;
  
  //rgb
}

}

void Screen::drawSprite(sint x, sint y, const Sprite& sprite, const Palette& palette, bool flipX, bool flipY, sint scale){

//scale always 2
sint width  = sprite.width;
sint height = sprite.height;

//current pos
sint pos = 0;

for(sint i = 0; i < height; i++){
  for(sint j = 0; j < width; j++, pos++){
      
    sint index = sprite.map[pos];
    
    if(index != 0){
      
      sint dx;
      sint dy;
      
      if(flipX){
          
        dx = width - 1 - j;  
      }
      else{
          
        dx = j;  
      }
      
      if(flipY){
          
        dy = height - 1 - i;  
      }
      else{
          
        dy = i;  
      } 

      sint px = x + scale * dx;
      sint py = y + scale * dy;       
      
          
      if(scale == 1){
        
        setPixel(px, py, palette.colors[index-1]);      
      }
      else if(scale == 2){
          
        setPixel(px  , py  , palette.colors[index-1]);  
        setPixel(px+1, py  , palette.colors[index-1]);  
        setPixel(px  , py+1, palette.colors[index-1]);  
        setPixel(px+1, py+1, palette.colors[index-1]);              
      }
      else{
           
        drawRect(px, py, scale, scale, palette.colors[index-1]);    
      }   
    }
  }   
}

}

This is the main loop:

int main(int argc, char* argv[]) {

//graphic stuff
screen.init();

consoleInit(GFX_BOTTOM, NULL);

//random
srand(time(NULL));

//level init
level.loadLevel("Level_1.bin");


// Main loop
while(aptMainLoop())
{
    
  screen.update();
  
  //steer and movement
  if(tick){
                    
    for(usint i=0;i<entities.size();i++){
        
      entities[i]->steer();
      entities[i]->move();
      entities[i]->animate();
    }
    
    //update Timer      
    level.updateTimer();

    //update Cam
    level.updateCam();      
  }     
  tick = !tick; 

  //draw map
  level.draw(tick);       
  
  
  for(usint i=0;i<entities.size();i++){
        
    entities[i]->draw();
  }  

  if(screen.kDown & KEY_Y) break;     
}
 
// Deinit libs
gfxExit();
return 0;

}

and screen.update:

void Screen::update(){

//screen
gfxFlushBuffers();
gfxSwapBuffers();   
gspWaitForVBlank();     

map = gfxGetFramebuffer(source, GFX_LEFT, NULL, NULL);  

memset(map, 0, width * height * 2);


//buttons
hidScanInput();
kDown = hidKeysDown();
kHeld = hidKeysHeld();

}

I'd be thankful if anyone could help me.

0

There are 0 answers