Ejemplo n.º 1
0
void chip8emu::Chip8Emu::render()
{
   if(mGfx->isDrawFlagSet()) {
      std::uint16_t pixelsOn = 0;
      for (int y = 0; y < mGfx->height(); y++) {
         for (int x = 0; x < mGfx->width(); x++) {
            if ((*mGfx)[y * mGfx->width() + x]) {
               mPixelRects[pixelsOn].x = mScale * x, mPixelRects[pixelsOn].y = mScale * y;
               pixelsOn++;
            }
         }
      }

      // Clear the screen then draw the pixels
      SDL_SetRenderDrawColor(mRenderer.get(), 0x00, 0x00, 0x00, 0xFF);
      SDL_RenderClear(mRenderer.get());
      SDL_SetRenderDrawColor(mRenderer.get(), 0xE0, 0xEE, 0xEE, 0xFF);
      SDL_RenderFillRects(mRenderer.get(), mPixelRects, pixelsOn);

      // Flip the screen and hold
      SDL_RenderPresent(mRenderer.get());

      mGfx->resetDrawFlag();
   }
}
Ejemplo n.º 2
0
int screen_drawFillRects(SDL_Renderer* renderer, SDL_Rect* rects, int count)
{
	if( SDL_RenderFillRects(renderer,rects, count) != 0 )
	{
		LOG("draw rects fail: %s", SDL_GetError());
		return 1;
	}
	return 0;
}
Ejemplo n.º 3
0
void MusicTile::render(SDL_Renderer* screen, int scale)
{
	if (row_ >= 5)
		offsetX = scale;
	else
		offsetX = 0;
	if (tapped)
	{
		SDL_Rect tiles_[4];

		tiles_[0] = { //top left
			row_ * (scale + 1) + offsetX,
			(int)height_, 
			(scale / 2) - explode_ * 2,
			(scale / 4) - explode_
		};

		tiles_[1] = { //bottom left
			row_ * (scale + 1) + offsetX,
			(int)height_ + (scale / 4) + explode_,
			(scale / 2) - explode_ * 2,
			(scale / 4) - explode_
		};

		tiles_[2] = { //bottom right
			(int)(row_ * (scale + 1) + (scale / 2) + explode_ * 2) + offsetX,
			(int)height_ + (scale / 4) + explode_,
			(int)((scale / 2) - explode_ * 2),
			(int)((scale / 4) - explode_)
		};

		tiles_[3] = { //top right
			(int)(row_ * (scale + 1) + (scale / 2) + explode_ * 2) + offsetX,
			(int)height_,
			(int)((scale / 2) - explode_ * 2),
			(int)((scale / 4) - explode_)
		};

		SDL_SetRenderDrawColor(screen, (int)(color_.r + explode_ / 8.f * (255 - color_.r)), (int)(color_.g + explode_ / 8.f * (255 - color_.g)), (int)(color_.b + explode_ / 8.f * (255 - color_.b)), 255);

		SDL_RenderFillRects(screen, tiles_, 4);
	}
	else
	{
		SDL_Rect tile_ = { row_ * (scale + 1) + offsetX, (int)height_, scale, scale / 2 };
		SDL_SetRenderDrawColor(screen, color_.r, color_.g, color_.b, 0xFF);
		SDL_RenderFillRect(screen, &tile_);
	}
}
Ejemplo n.º 4
0
CAMLprim value
caml_SDL_RenderFillRects(value renderer, value ml_rects)
{
    unsigned int i;
    unsigned int count = Wosize_val(ml_rects);
    SDL_Rect * rects = malloc(count * sizeof(SDL_Rect));
    for (i = 0; i < count; i++) {
        value _rect = Field(ml_rects, i);
        SDL_Rect_val(&(rects[i]), _rect);
    }
    int r = SDL_RenderFillRects(
                SDL_Renderer_val(renderer),
                rects, count);
    free(rects);
    if (r) caml_failwith("Sdlrender.fill_rects");
    return Val_unit;
}
Ejemplo n.º 5
0
void NXSurface::DrawRect(int x1, int y1, int x2, int y2, uint8_t r, uint8_t g, uint8_t b)
{
	if (this != screen)
		SetAsTarget(true);

	SDL_Rect rects[4] = {
		{x1 * SCALE, y1 * SCALE, ((x2 - x1) + 1) * SCALE, SCALE},
		{x1 * SCALE, y2 * SCALE, ((x2 - x1) + 1) * SCALE, SCALE},
		{x1 * SCALE, y1 * SCALE, SCALE,                   ((y2 - y1) + 1) * SCALE},
		{x2 * SCALE, y1 * SCALE, SCALE,                   ((y2 - y1) + 1) * SCALE}
	};

	SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
	SDL_RenderFillRects(renderer, rects, 4);

	if (this != screen)
		SetAsTarget(false);
}
Ejemplo n.º 6
0
/*
IDEA FOR BALL DRAWING:
SINCE EVERYTHING IS DRAWN WITH RECTS:
CREATE A VENEER FOR THE RENDERFILLRECT FUNTIONS THAT STORES THE RECT INTO A LIST ON EACH MAIN DRAW PASS
ONCE YOU HAVE THE LIST (I.E. MUST DRAW BALL LAST), USE THAT LIST OF RECTS TO INTERSECT THE BALL AND INVERT COLOR IN THE INTERSECTED AREA.
*/
void CBall::draw(SDL_Renderer* r, const std::vector<SDL_Rect> &drawnBoxes) {
	// draw ball
	CRectEntity::draw(r);
	// draw inverted over intersects
	std::vector<SDL_Rect> intersects;
	for (auto box : drawnBoxes) {
		SDL_Rect intersection;
		if (SDL_IntersectRect(&_boundingBox, &box, &intersection))
			intersects.push_back(intersection);
	}
	if (!intersects.empty()) {
		// keep old color
		SDL_Color save;
		SDL_GetRenderDrawColor(r, &save.r, &save.g, &save.b, &save.a);
		// draw colored rectangle
		SDL_SetRenderDrawColor(r, 255 - _drawColor.r, 255 - _drawColor.g, 255 - _drawColor.b, SDL_ALPHA_OPAQUE);
		SDL_RenderFillRects(r, &intersects[0], intersects.size());
		// reset color
		SDL_SetRenderDrawColor(r, save.r, save.g, save.b, save.a);
	}
}
Ejemplo n.º 7
0
//Draws multiple filled rectangles
//param:rectangles->pointer to an array of rectangles
//param:numberOfRectangles->Number of rectangles in the array
//param:color->The color to fill the rectangles with
//returns 0 for success, -1 for error
int SpriteBatch::DrawFilledRects(SDL_Rect* rectangles, int numberOfRectangles, SDL_Color color)
{
	//Check if spritebatch.begin has been called
	if(!begun)
	{
		std::cout<<"Begin must be called before attempting to draw"<<std::endl;
		return -1;
	}

	//Make sure number of rectangles is valid
	if(numberOfRectangles <= 0)
	{
		std::cout<<"DrawFilledRects error: Size must be greater than 0"<<std::endl;
		return -1;
	}

	int result = 0;

	//Set the draw color
	result = SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);

	//Check for color setting problems
	if(result != 0)
	{
		std::cout<<"DrawFilledRects error: Problem setting rectangle color" << std::endl;
		return result;
	}

	result = SDL_RenderFillRects(renderer, rectangles, numberOfRectangles);

	if(result != 0)
	{
		std::cout<<"DrawFilledRects error: Problem drawing rectangles"<<std::endl;
		return result;
	}

	return result;
}
Ejemplo n.º 8
0
void GA::Execute(){
	std::cout << "Solving TSP\n";
		SDL_Window *window = NULL;
	//SDL_Surface *surface;
	SDL_Init(SDL_INIT_VIDEO);
	const int wWidth = 640;
	const int wHeight = 480;
	window = SDL_CreateWindow("TSP Visualizer",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, wWidth,wHeight,SDL_WINDOW_OPENGL);
	//surface = SDL_GetWindowSurface(window);
	SDL_UpdateWindowSurface(window);
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	std::vector<SDL_Rect> cityDots;
	for(const auto& c : baseCityVector){
		SDL_Rect dot;
		dot.x = c.getX() * wWidth / maxXPosition - 2;
		dot.y = c.getY() * wHeight / maxYPosition - 2;
		dot.w = 4;
		dot.h = 4;
		cityDots.push_back(dot);
	}

	for(int g = 1; g < targetGenerationNumber; g++){
		currentGeneration = g;
		std::cout << "Current generation: " << g << "\n";
		SortCandidates();
		//std::cout << "Sorted population\n";
		parentsPopulation.clear();
		SelectParents(populationBreadersPercentage);
		int childrenPopulationSize = populationSize * populationBreadersPercentage / 100;	
		//std::cout << "Childrem population size: " << childrenPopulationSize << "\n";
		while(newPopulation.size() < childrenPopulationSize){
			//std::cout << "New pop size: " << newPopulation.size() << "\n";
			auto parent1 = parentsPopulation.at( std::rand() % parentsPopulation.size() );
			auto parent2 = parentsPopulation.at( std::rand() % parentsPopulation.size() );
			
			//std::future<Candidate> fChild1 = std::async(GA::PMXCrossover,parent1,parent2);
			
			Candidate child1 = PMXCrossover(parent1,parent2);
			Candidate child2 = PMXCrossover(parent2,parent1);
			//if(std::find(newPopulation.begin(),newPopulation.end(),child) == newPopulation.end())
			newPopulation.push_back ( child1 );
			newPopulation.push_back ( child2 );
		}
		//std::cout << "Created new population\n";
		for(int m = 1; m <= newPopulation.size() * mutationPercentage / 100; m++){
			//auto mutant = newPopulation[std::rand() % newPopulation.size()];
			auto mutant = newPopulation[ (std::rand() * m) % newPopulation.size()];
			Mutate(mutant, mutationPower);
			mutant.ReEvaluate(distances);
		}
		//std::cout << "Mutated population\n";
		population.insert(population.end(), newPopulation.begin(), newPopulation.end());
		newPopulation.clear();
		NormalizeCandidates();
		SortCandidates();
		population = std::vector<Candidate>(population.begin(),population.begin()+populationSize);
		std::cout << "Best: " << population.at(0) << "\n";
		
		SDL_SetRenderDrawColor(renderer, 255,255,255,255);
		SDL_RenderClear(renderer);
				
		std::vector<SDL_Point> cityPositions;
				/*
		int maxX;
		int maxY;
		for(const auto& city : getBest().getCandidate()){
			maxX = 0;
			maxY = 0;
			if(city.getX() > maxX)
				maxX = city.getX();
			if(city.getY() > maxY)
				maxY = city.getY();
		}	
		*/
		
		for(const auto& city : population[0].getCandidate()){
			int posY = city.getY() * wHeight / maxYPosition;
			int posX = city.getX() * wWidth / maxXPosition;

			SDL_Point position;
			position.y = city.getY() * wHeight / maxYPosition;
			position.x = city.getX() * wWidth / maxXPosition;
			cityPositions.push_back(position);

			
		}
		SDL_SetRenderDrawColor(renderer, 255,0,0,255);

		SDL_RenderDrawLines(renderer, &cityPositions[0], cityPositions.size());
		SDL_SetRenderDrawColor(renderer, 0,255,0,255);
		SDL_RenderFillRects(renderer, &cityDots[0], cityDots.size());
		
		SDL_RenderPresent(renderer);
		
		/*
		if(renderer == NULL){
			SDL_RenderDrawLine(renderer, g*10, g, 250, 250);	
		}
		*/
	}
	SDL_DestroyWindow(window);
	SDL_Quit();

}
Ejemplo n.º 9
0
int main(int argc, char **argv)
{
    int done = 0;
    SDL_Window *window;
    SDL_Renderer *renderer;

    /* !!! FIXME: check for errors. */
    SDL_Init(SDL_INIT_VIDEO);
    window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE);
    renderer = SDL_CreateRenderer(window, -1, 0);

    if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
        SDL_Log("Enabling hit-testing failed!\n");
        SDL_Quit();
        return 1;
    }

    while (!done)
    {
        SDL_Event e;
        int nothing_to_do = 1;

        SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
        SDL_RenderClear(renderer);
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
        SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
        SDL_RenderPresent(renderer);

        while (SDL_PollEvent(&e)) {
            nothing_to_do = 0;

            switch (e.type)
            {
                case SDL_MOUSEBUTTONDOWN:
                    SDL_Log("button down!\n");
                    break;

                case SDL_MOUSEBUTTONUP:
                    SDL_Log("button up!\n");
                    break;

                case SDL_WINDOWEVENT:
                    if (e.window.event == SDL_WINDOWEVENT_MOVED) {
                        SDL_Log("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
                    }
                    break;

                case SDL_KEYDOWN:
                    if (e.key.keysym.sym == SDLK_ESCAPE) {
                        done = 1;
                    } else if (e.key.keysym.sym == SDLK_x) {
                        if (!areas) {
                            areas = drag_areas;
                            numareas = SDL_arraysize(drag_areas);
                        } else {
                            areas = NULL;
                            numareas = 0;
                        }
                    }
                    break;

                case SDL_QUIT:
                    done = 1;
                    break;
            }
        }

        if (nothing_to_do) {
            SDL_Delay(50);
        }
    }

    SDL_Quit();
    return 0;
}
Ejemplo n.º 10
0
static void
DrawRects(SDL_Renderer * renderer)
{
    SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
    SDL_RenderFillRects(renderer, rects, num_rects);
}