C# Tetris game slow performance -


i'm programming tetris clone c# school project. using microsoft visual studio 2012. game implemented 2 dimensional array of blocks(list of lists of blocks) , every block has own texture (bmp image). drawing whole array onto picturebox control , problem starts. when updating image on picturebox (moving/rotating active shape) game lags. tried draw on panel control instead result same. have rough idea might cause lag don't know how rid of it.

this draw method of game "grid":

public void draw(graphics g) {    brush brush;    font font = new system.drawing.font( "arial", 5);    (int = 0; < width; i++)      (int j = 0; j < height; j++)      {           brush = new texturebrush(blocks[i][j].texture);           if (blocks[i][j].occupied==true)              g.fillrectangle(brush, * 20, j * 20, * 20 + blocks[i][j].texture.width, j * 20 + blocks[i][j].texture.height);       } } 

this draw method of active tetromino:

public void draw(graphics g) {     brush brush = new texturebrush(blocks[0].texture);     foreach (fullblock b in blocks)        g.fillrectangle(brush, b.x * 20, b.y * 20,b.texture.width, b.texture.height); } 

the game use both of them (double buffering attempt):

public void gamedraw(picturebox p) {     graphics g = graphics.fromimage(gb);     gamegrid.draw(g);     playingshape.draw(g);     p.image = gb;     p.refresh(); } 

where "gb" private bitmap variable create once in class constructor (to reduce (unsuccessfully) lag).

the gamedraw method called whenever state of game changed (e.g. moving/rotating active tetromino , every "gravity" tick)

no need picture box, add own control:

using system.drawing; using system.windows.forms;  namespace tetrisgame {     public sealed class tetriscontrol : control     {         private theblocktype[][] blocks = ...;          protected override void onpaint(painteventargs e)         {             //draw stuff here direct control, no buffers in middle             //if causes flickering, turn on double buffering, don't bother doing             //this existing draw method:             draw(e.graphics);         }     } } 

then on every tick or movement, not call paint, invalidate control:

tetris.invalidate(); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -