<commit_before>/*-
 * Copyright (c) 2013, 2014 <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <SDL.h>
#include <SDL_image.h>

#include <string.h>
#include <glew.h>

#include "../bsd.h"

#include "../alloc.h"
#include "../micromp.h"
#include "../gl/marshal.h"
#include "../gl/shaders.h"
#include "canvas.h"
#include "parchment.h"

#define PARCHMENT_DIM 2048
#define PARCHMENT_MASK (PARCHMENT_DIM-1)
#define PARCHMENT_FILE "share/extern/parchment.jpg"

static GLuint texture;
static GLuint postprocess_tex;
static glm_slab_group* glmsg;
static void parchment_activate(void*);

struct parchment_s {
  unsigned tx, ty;
};

void parchment_init(void) {
  SDL_Surface* orig_surf, * new_surf;
  canvas canv;

  orig_surf = IMG_Load(PARCHMENT_FILE);
  if (!orig_surf)
    errx(EX_DATAERR, "File " PARCHMENT_FILE " is could not be loaded: %s",
         IMG_GetError());

  if (PARCHMENT_DIM != orig_surf->w || PARCHMENT_DIM != orig_surf->h)
    errx(EX_DATAERR, "File " PARCHMENT_FILE " is of the wrong dimensions.");

  new_surf = SDL_ConvertSurface(orig_surf, screen_pixel_format, 0);
  if (!new_surf)
    errx(EX_SOFTWARE, "Could not convert parchment surface: %s",
         SDL_GetError());

  /* new_surf is now the same format as a canvas, so we can directly point to
   * the data inside the surface.
   */
  canv.w = PARCHMENT_DIM;
  canv.h = PARCHMENT_DIM;
  canv.pitch = PARCHMENT_DIM;
  canv.logical_width = PARCHMENT_DIM;
  canv.ox = canv.oy = 0;
  canv.px = (canvas_pixel*)new_surf->pixels;
  canv.depth = NULL;

  texture = canvas_to_texture(&canv, 0);
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

  SDL_FreeSurface(new_surf);
  SDL_FreeSurface(orig_surf);

  glmsg = glm_slab_group_new(parchment_activate, NULL, NULL,
                             shader_fixed_texture_configure_vbo,
                             sizeof(shader_fixed_texture_vertex));

  glGenTextures(1, &postprocess_tex);
}

parchment* parchment_new(void) {
  parchment* this = xmalloc(sizeof(parchment));
  this->tx = this->ty = 0;
  return this;
}

void parchment_delete(parchment* this) {
  free(this);
}

static void parchment_activate(void* ignore) {
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, texture);
  shader_fixed_texture_activate(NULL);
}

void parchment_draw(canvas* dst, const parchment* this) {
  shader_fixed_texture_vertex* vertices;
  unsigned short* indices, base;
  coord_offset x, y, x0, y0;
  glm_slab* slab = glm_slab_get(glmsg);

  x0 = this->tx / 1024 & PARCHMENT_MASK;
  if (x0 > 0) x0 -= PARCHMENT_DIM;
  y0 = this->ty / 1024 & PARCHMENT_MASK;
  if (y0 > 0) y0 -= PARCHMENT_DIM;

  for (y = y0; y < (signed)dst->h; y += PARCHMENT_DIM) {
    for (x = x0; x < (signed)dst->w; x += PARCHMENT_DIM) {
      base = GLM_ALLOC(&vertices, &indices, slab, 4, 6);
      vertices[0].v[0] = x;
      vertices[0].v[1] = y;
      vertices[0].v[2] = 4095*METRE;<commit_msg>Remove useless glEnable(GL_TEXTURE_2D) from parchment.<commit_after>/*-
 * Copyright (c) 2013, 2014 <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <SDL.h>
#include <SDL_image.h>

#include <string.h>
#include <glew.h>

#include "../bsd.h"

#include "../alloc.h"
#include "../micromp.h"
#include "../gl/marshal.h"
#include "../gl/shaders.h"
#include "canvas.h"
#include "parchment.h"

#define PARCHMENT_DIM 2048
#define PARCHMENT_MASK (PARCHMENT_DIM-1)
#define PARCHMENT_FILE "share/extern/parchment.jpg"

static GLuint texture;
static GLuint postprocess_tex;
static glm_slab_group* glmsg;
static void parchment_activate(void*);

struct parchment_s {
  unsigned tx, ty;
};

void parchment_init(void) {
  SDL_Surface* orig_surf, * new_surf;
  canvas canv;

  orig_surf = IMG_Load(PARCHMENT_FILE);
  if (!orig_surf)
    errx(EX_DATAERR, "File " PARCHMENT_FILE " is could not be loaded: %s",
         IMG_GetError());

  if (PARCHMENT_DIM != orig_surf->w || PARCHMENT_DIM != orig_surf->h)
    errx(EX_DATAERR, "File " PARCHMENT_FILE " is of the wrong dimensions.");

  new_surf = SDL_ConvertSurface(orig_surf, screen_pixel_format, 0);
  if (!new_surf)
    errx(EX_SOFTWARE, "Could not convert parchment surface: %s",
         SDL_GetError());

  /* new_surf is now the same format as a canvas, so we can directly point to
   * the data inside the surface.
   */
  canv.w = PARCHMENT_DIM;
  canv.h = PARCHMENT_DIM;
  canv.pitch = PARCHMENT_DIM;
  canv.logical_width = PARCHMENT_DIM;
  canv.ox = canv.oy = 0;
  canv.px = (canvas_pixel*)new_surf->pixels;
  canv.depth = NULL;

  texture = canvas_to_texture(&canv, 0);
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

  SDL_FreeSurface(new_surf);
  SDL_FreeSurface(orig_surf);

  glmsg = glm_slab_group_new(parchment_activate, NULL, NULL,
                             shader_fixed_texture_configure_vbo,
                             sizeof(shader_fixed_texture_vertex));

  glGenTextures(1, &postprocess_tex);
}

parchment* parchment_new(void) {
  parchment* this = xmalloc(sizeof(parchment));
  this->tx = this->ty = 0;
  return this;
}

void parchment_delete(parchment* this) {
  free(this);
}

static void parchment_activate(void* ignore) {
  glBindTexture(GL_TEXTURE_2D, texture);
  shader_fixed_texture_activate(NULL);
}

void parchment_draw(canvas* dst, const parchment* this) {
  shader_fixed_texture_vertex* vertices;
  unsigned short* indices, base;
  coord_offset x, y, x0, y0;
  glm_slab* slab = glm_slab_get(glmsg);

  x0 = this->tx / 1024 & PARCHMENT_MASK;
  if (x0 > 0) x0 -= PARCHMENT_DIM;
  y0 = this->ty / 1024 & PARCHMENT_MASK;
  if (y0 > 0) y0 -= PARCHMENT_DIM;

  for (y = y0; y < (signed)dst->h; y += PARCHMENT_DIM) {
    for (x = x0; x < (signed)dst->w; x += PARCHMENT_DIM) {
      base = GLM_ALLOC(&vertices, &indices, slab, 4, 6);
      vertices[0].v[0] = x;
      vertices[0].v[1] = y;
      vertices[0].v[2] = 4095*METRE;
{'fraction_non_alphanumeric': 0.1016839378238342, 'fraction_numerical': 0.012953367875647668, 'mean_word_length': 4.167317345231456, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '85740', 'n_tokens_mistral': 3364, 'n_tokens_neox': 3106, 'n_words': 936}